pythonjava調用
你使用jython這個解釋器就可以讓python直接調用java, 調用完成後,你用python封裝成一個服務。其它的python程序員就可以間接調用java對象了。
jython調用java這個方式也被eclipse+pydev使用,是目前最直接的方法。
② 如何在 Java 中調用 Python 代碼
可以用Python的擴展來實現。Python本來是C實現的,封裝二進制兼容的C++是很容易的。Java的話得通過JNI來實現,就是說在Python擴展里用C調用Java。另外,也可以寫一個TCP服務來包裝C++/Java的介面,通過網路來調用,這樣更通用。
③ 怎麼在java的flink中調用python程序
一、在java類中直接執行python語句
import org.python.util.PythonInterpreter;
public class FirstJavaScript {
public static void main(String args[]) {
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.exec("days=('mod','Tue','Wed','Thu','Fri','Sat','Sun'); ");
interpreter.exec("print days[1];");
}// main
}
調用的結果是Tue,在控制台顯示出來,這是直接進行調用的。
二、在java中調用本機python腳本中的函數
首先建立一個python腳本,名字為:my_utils.py
def adder(a, b):
return a + b
然後建立一個java類,用來測試,
java類代碼 FirstJavaScript:
import org.python.core.PyFunction;
import org.python.core.PyInteger;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;
public class FirstJavaScript {
public static void main(String args[]) {
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("C:\\Python27\\programs\\my_utils.py");
PyFunction func = (PyFunction) interpreter.get("adder",
PyFunction.class);
int a = 2010, b = 2;
PyObject pyobj = func.__call__(new PyInteger(a), new PyInteger(b));
System.out.println("anwser = " + pyobj.toString());
}// main
}
得到的結果是:anwser = 2012
三、使用java直接執行python腳本
建立腳本inputpy
#open files
print 'hello'
number=[3,5,2,0,6]
print number
number.sort()
print number
number.append(0)
print number
print number.count(0)
print number.index(5)
建立java類,調用這個腳本:
import org.python.util.PythonInterpreter;
public class FirstJavaScript {
public static void main(String args[]) {
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("C:\\Python27\\programs\\input.py");
}// main
}
得到的結果是:
hello
[3, 5, 2, 0, 6]
[0, 2, 3, 5, 6]
[0, 2, 3, 5, 6, 0]
2
3
④ java調用python時傳遞的參數問題
需要用到需要用到jython.jar
java example:
public static void main(String[] args) {
//定義參數
String[] args2 = {"arg1","arg2"};
//設置參數
PythonInterpreter.initialize(null, null, args2);
PythonInterpreter interpreter = new PythonInterpreter();
//執行
interpreter.execfile("E:\\jython.py");
System.out.println("----------run over!----------");
}
python的程序:
#!/bin/env python
import time
import sys
argCount = len(sys.argv)
print('before sleep')
time.sleep(5);
print('after sleep')
for str in sys.argv:
print(str)
⑤ 有沒有從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'
如果解決了您的問題請採納!
如果未解決請繼續追問!
⑦ 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'
如果解決了您的問題請採納!
⑧ 如何在java中調用python
package com.lyz.test.jython;
import org.python.util.PythonInterpreter;
/**
* 第一個Jython程序
* @author liuyazhuang
*
*/
public class FirstJythonScript {
public static void main(String args[]) {
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.exec("days=('mod','Tue','Wed','Thu','Fri','Sat','Sun'); ");
interpreter.exec("print days[1];");
}
}
⑨ 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'