導航:首頁 > 編程語言 > java語言變成python

java語言變成python

發布時間:2023-10-07 16:30:55

Ⅰ 如何在java中調用python代碼

Jython(原JPython),是一個用Java語言寫的Python解釋器。 在沒有第三方模塊的情況下,通常選擇利用Jython來調用Python代碼, 它是一個開源的JAR包,你可以到官網下載 一個HelloPython程序 importorg.python.util.PythonInterpreter; publicclassHelloPython{ publicstaticvoidmain(String[]args){ PythonInterpreterinterpreter=newPythonInterpreter(); interpreter.exec("print('hello')"); } } 什麼是PythonInterpreter?它的中文意思即是「Python解釋器」。我們知道Python程序都是通過解釋器來執行的,我們在Java中創建一個「解釋器」對象,模擬Python解釋器的行為,通過exec("Python語句")直接在JVM中執行Python代碼,上面代碼的輸出結果為:hello 在Jvm中執行Python腳本 interpreter.execfile("D:/labs/mytest/hello.py"); 如上,將exec改為execfile就可以了。需要注意的是,這個.py文件不能含有第三方模塊,因為這個「Python腳本」最終還是在JVM環境下執行的,如果有第三方模塊將會報錯:javaImportError:Nomolenamedxxx 僅在Java中調用Python編寫的函數 先完成一個hello.py代碼: defhello(): return'Hello' 在Java代碼中調用這個函數: importorg.python.core.PyFunction; importorg.python.core.PyObject; importorg.python.util.PythonInterpreter; publicclassHelloPython{ publicstaticvoidmain(String[]args){ PythonInterpreterinterpreter=newPythonInterpreter(); interpreter.execfile("D:/labs/hello.py"); PyFunctionpyFunction=interpreter.get("hello",PyFunction.class);//第一個參數為期望獲得的函數(變數)的名字,第二個參數為期望返回的對象類型 PyObjectpyObject=pyFunction.__call__();//調用函數 System.out.println(pyObject); } } 上面的代碼執行結果為:Hello 即便只是調用一個函數,也必須先載入這個.py文件,之後再通過Jython包中所定義的類獲取、調用這個函數。 如果函數需要參數,在Java中必須先將參數轉化為對應的「Python類型」,例如: __call__(newPyInteger(a),newPyInteger(b)) a,b的類型為Java中的int型,還有諸如:PyString(Stringstring)、PyList(Iteratoriter)等。 詳細可以參考官方的api文檔。 包含第三方模塊的情況:一個手寫識別程序 這是我和舍友合作寫的一個小程序,完整代碼在這里:,界面上引用了corejava上的一段代碼。Python代碼是舍友寫的,因為在Python程序中使用了第三方的NumPy模塊,導致無法通過Jython執行。下面這個方法純粹是個人思路,沒有深入查資料。核心代碼如下: importjava.io.*; classPyCaller{ privatestaticfinalStringDATA_SWAP="temp.txt"; privatestaticfinalStringPY_URL=System.getProperty("user.dir")+"\test.py"; (Stringpath){ PrintWriterpw=null; try{ pw=newPrintWriter(newFileWriter(newFile(DATA_SWAP))); }catch(IOExceptione){ e.printStackTrace(); } pw.print(path); pw.close(); } publicstaticStringreadAnswer(){ BufferedReaderbr; Stringanswer=null; try{ br=newBufferedReader(newFileReader(newFile(DATA_SWAP))); answer=br.readLine(); }catch(FileNotFoundExceptione){ e.printStackTrace(); }catch(IOExceptione){ e.printStackTrace(); } returnanswer; } publicstaticvoidexecPy(){ Processproc=null; try{ proc=Runtime.getRuntime().exec("python"+PY_URL); proc.waitFor(); }catch(IOExceptione){ e.printStackTrace(); }catch(InterruptedExceptione){ e.printStackTrace(); } } //測試碼 publicstaticvoidmain(String[]args)throwsIOException,InterruptedException{ writeImagePath("D:\labs\mytest\test.jpg"); execPy(); System.out.println(readAnswer()); } } 實際上就是通過Java執行一個命令行指令。

Ⅱ 如何在java工程里運行一個python腳本

可以使用jython


方法參考如下

PythonInterpreter interpreter = new PythonInterpreter();

interpreter.execfile("uar/local/xxx.py");

//pyFunction :py中方法名

PyFunction func = (PyFunction)interpreter.get("pyFunction",PyFunction.class);

Integer a = 1

Integer b = 2

// py中方法傳參

PyObject pyobj = func.__call__(new PyInteger(a), new PyInteger(b));

System.out.println("anwser = " + pyobj.toString());

閱讀全文

與java語言變成python相關的資料

熱點內容
手機文件圖片怎麼解壓 瀏覽:655
分布式系統設計pdf 瀏覽:181
中軸線趨勢指標源碼 瀏覽:721
labview源碼代碼 瀏覽:61
15匹谷輪壓縮機圖片 瀏覽:818
bsp分割演算法 瀏覽:22
高手選股公式源碼 瀏覽:682
脆皮軟心球解壓視頻十分鍾 瀏覽:404
如何關閉蘋果app同步充值 瀏覽:766
視頻壓縮發送微信 瀏覽:856
程序員段子大全圖片 瀏覽:760
javaudp心跳 瀏覽:429
阿里賣家app如何分配詢盤 瀏覽:828
如何拔打中國移動人工伺服器 瀏覽:505
傳奇歸來為什麼連不上伺服器 瀏覽:555
壓縮機潤滑油在哪個位置工作 瀏覽:141
張翰解壓減幾 瀏覽:775
你好法語1教師用書pdf 瀏覽:486
手機解壓縮後文件在哪 瀏覽:472
linux內核框架 瀏覽:720