本文目錄一覽:
python怎麼調用java程序
把java封裝成restful介面,然後python通過遠程調用數據。
使用Pyjnius這個python庫。
#源代碼:github.com/kivy/pyjnius
#文檔:pyjnius.readthedocs.org
#也有其他一些的庫,如 JPype 或 Py4j ,它們在設計和可用性方面都不是很好。而使用 Jython也不為另一種選擇,因為我們想使用 python開發Android項目。
#現在就讓我來告訴你,如何簡單的使用Pyjnius:
from jnius import autoclass
Stack = autoclass(‘java.util.Stack’)
stack = Stack()
stack.push(‘hello’)
stack.push(‘world’)
stack.pop()
‘world’
stack.pop()
‘hello’
有沒有從Python調用Java的好方法
[java] view plain copy
String[] arg = new String[] {“python”,types,parameter};//第一個參數是python解釋器位置,第二個參數是執行的python腳本位置,接下來的都是參數
Process process = Runtime.getRuntime().exec(arg);
InputStreamReader inputStreamReader = new InputStreamReader(process.getInputStream());
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String line = “”;
StringBuffer stringBuffer = new StringBuffer();
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
stringBuffer.append(“\n”);
}
bufferedReader.close();
process.waitFor();
python 調用java對象
你使用jython這個解釋器就可以讓python直接調用java, 調用完成後,你用python封裝成一個服務。其它的python程序員就可以間接調用java對象了。
jython調用java這個方式也被eclipse+pydev使用,是目前最直接的方法。
原創文章,作者:NMYYT,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/330919.html