Python和java?如何相互調(diào)用
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)用本機(jī)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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
pytorch實(shí)現(xiàn)保證每次運(yùn)行使用的隨機(jī)數(shù)都相同
今天小編就為大家分享一篇pytorch實(shí)現(xiàn)保證每次運(yùn)行使用的隨機(jī)數(shù)都相同,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-02-02利用python3隨機(jī)生成中文字符的實(shí)現(xiàn)方法
最近在學(xué)習(xí)python3,發(fā)現(xiàn)網(wǎng)上關(guān)于ptyhon3隨機(jī)生成中文的資料非常少,所以決定將自己實(shí)現(xiàn)的方法分享下,下面這篇文章主要給大家介紹了關(guān)于利用python3隨機(jī)生成中文字符的實(shí)現(xiàn)方法,需要的朋友可以參考借鑒,下面來一起看看吧。2017-11-11Python中cv2.Canny() 函數(shù)使用方法
cv2.Canny() 函數(shù)是 OpenCV 中的邊緣檢測函數(shù)之一,用于檢測圖像的邊緣,它的基本原理是通過計(jì)算圖像中每個(gè)像素點(diǎn)的梯度值來檢測邊緣,本文通過示例代碼介紹Python中cv2.Canny() 函數(shù)用法,需要的朋友參考下吧2023-07-07python對(duì)矩陣進(jìn)行轉(zhuǎn)置的2種處理方法
這篇文章主要介紹了python對(duì)矩陣進(jìn)行轉(zhuǎn)置的2種處理方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07ITK 實(shí)現(xiàn)多張圖像轉(zhuǎn)成單個(gè)nii.gz或mha文件案例
這篇文章主要介紹了ITK 實(shí)現(xiàn)多張圖像轉(zhuǎn)成單個(gè)nii.gz或mha文件案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07