當前位置:首頁 » 編程語言 » python執行java代碼

python執行java代碼

發布時間: 2022-12-09 15:45:11

㈠ 如何在python腳本中執行java 命令

用jpython不就可以了!

㈡ 有沒有從Python調用Java的好方法

[java]view plain

  • String[]arg=newString[]{"python",types,parameter};//第一個參數是python解釋器位置,第二個參數是執行的python腳本位置,接下來的都是參數

  • Processprocess=Runtime.getRuntime().exec(arg);

  • =newInputStreamReader(process.getInputStream());

  • BufferedReaderbufferedReader=newBufferedReader(inputStreamReader);

  • Stringline="";

  • StringBufferstringBuffer=newStringBuffer();

  • while((line=bufferedReader.readLine())!=null){

  • stringBuffer.append(line);

  • stringBuffer.append(" ");

  • }

  • bufferedReader.close();

  • process.waitFor();

㈢ python怎麼調用java程序

  • 把java封裝成restful介面,然後python通過遠程調用數據。

  • 使用Pyjnius這個python庫。

#源代碼:github.com/kivy/pyjnius
#文檔:pyjnius.readthedocs.org
#也有其他一些的庫,如JPype或Py4j,它們在設計和可用性方面都不是很好。而使用Jython也不為另一種選擇,因為我們想使用python開發Android項目。
#現在就讓我來告訴你,如何簡單的使用Pyjnius:
>>>fromjniusimportautoclass
>>>Stack=autoclass('java.util.Stack')
>>>stack=Stack()
>>>stack.push('hello')
>>>stack.push('world')
>>>stack.pop()
'world'
>>>stack.pop()
'hello'

㈣ 如何在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執行一個命令行指令。

熱點內容
best把槍密碼多少 發布:2025-05-15 20:13:42 瀏覽:547
android安裝程序 發布:2025-05-15 20:13:20 瀏覽:558
c語言跳出死循環 發布:2025-05-15 20:06:04 瀏覽:823
a19處理器相當於安卓哪個水平 發布:2025-05-15 20:05:29 瀏覽:638
榮耀9i安卓強行關機按哪個鍵 發布:2025-05-15 20:00:32 瀏覽:750
密碼鎖寫什麼最好 發布:2025-05-15 19:05:31 瀏覽:782
5的源碼是 發布:2025-05-15 19:04:07 瀏覽:719
c語言創建的源文件 發布:2025-05-15 18:54:08 瀏覽:611
3個數字密碼鎖有多少種 發布:2025-05-15 18:49:48 瀏覽:684
壓縮包手機打開 發布:2025-05-15 18:37:34 瀏覽:217