Python和java?如何相互調(diào)用
更新時間:2022年01月24日 17:14:51 作者:小瘋子
這篇文章主要介紹了Python和java?如何相互調(diào)用,下面文章見到那的對Python和java?相互調(diào)用的方法做了個小總結(jié),具有一定的參考價值,需要的小伙伴可以參考一下,希望對你有所幫助
1.Java調(diào)用Python的函數(shù)
在java類中直接執(zhí)行python語句
class="highlight"> import org.python.util.PythonInterpreter; /** * Created by wjf on 2018/3/16. */ public class java2python { public static void main(String[] args) { PythonInterpreter interpreter = new PythonInterpreter(); interpreter.exec("# coding=utf-8"); interpreter.exec("days=('mod','Tue','Wed','Thu','Fri','Sat','Sun'); "); interpreter.exec("print days[1];"); } }
2.在java中調(diào)用本機python腳本中的函數(shù)
public static void main(String[] args) { PythonInterpreter interpreter = new PythonInterpreter(); interpreter.execfile("C:\\Users\\wjf\\IdeaProjects\\javaTestPython\\src\\my_utils.py"); PyFunction func = interpreter.get("adder", PyFunction.class); int a = 2010, b = 8; PyObject pyobj = func.__call__(new PyInteger(a), new PyInteger(b)); System.out.println("anwser = " + pyobj.toString()); }
3.使用java直接執(zhí)行python腳本
public static void main(String[] args) { PythonInterpreter interpreter = new PythonInterpreter(); interpreter.execfile("C:\\Users\\wjf\\IdeaProjects\\javaTestPython\\src\\input.py"); }
到此這篇關(guān)于Python和java 如何相互調(diào)用的文章就介紹到這了,更多相關(guān)Python和java相互調(diào)用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
pytorch實現(xiàn)保證每次運行使用的隨機數(shù)都相同
今天小編就為大家分享一篇pytorch實現(xiàn)保證每次運行使用的隨機數(shù)都相同,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02Python中cv2.Canny() 函數(shù)使用方法
cv2.Canny() 函數(shù)是 OpenCV 中的邊緣檢測函數(shù)之一,用于檢測圖像的邊緣,它的基本原理是通過計算圖像中每個像素點的梯度值來檢測邊緣,本文通過示例代碼介紹Python中cv2.Canny() 函數(shù)用法,需要的朋友參考下吧2023-07-07ITK 實現(xiàn)多張圖像轉(zhuǎn)成單個nii.gz或mha文件案例
這篇文章主要介紹了ITK 實現(xiàn)多張圖像轉(zhuǎn)成單個nii.gz或mha文件案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07