public static void main(String[] args) throws IOException {
// set up the command and parameter
String pythonScriptPath = "/home/norbert/python/helloPython.py";
String[] cmd = new String[2];
cmd[0] = "python2.6";
cmd[1] = pythonScriptPath;
// create runtime to execute external command
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(cmd);
// retrieve output from python script
BufferedReader bfr = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line = "";
while((line = bfr.readLine()) != null) {
// display each output line form python script
System.out.println(line);
}
}
python部分擴展包不多的話可以考慮jython,目前這種方式比較推薦,但是jython和python有一定的兼容性問題
還可以用JNI,cython,通過java調C,C調python實現
今天遇到Java調用一個Python腳本的問題,糾結了大半天,遇到各種問題。網上搜索的大部分都是用jython,但是我想要調用的python腳本里有import urllib,這個urllib也不是什麼第三方擴展庫,在python的安裝path下的Lib下就有,在python命令行下肯定是能找到的。但是用jython的話,sys的path裏面就太少了。示例代碼:
- import org.python.core.Py;
- import org.python.core.PySystemState;
- import org.python.util.PythonInterpreter;
- public class Test3 {
- /**
- * @param args
- */
- public static void main(String[] args) {
- PythonInterpreter interpreter = new PythonInterpreter();
- PySystemState sys = Py.getSystemState();
- //sys.path.add("D:\\jython2.5.2\\Lib");
- System.out.println(sys.path.toString());
- interpreter.exec("print 'hello'");
- interpreter.exec("import sys");
- interpreter.exec("print sys.path");
- // interpreter.exec("import urllib");
- // interpreter.exec("print urllib");
- }
- }
打印出來的sys.path爲:
- ['D:\\eclipse_jee_workspace\\ZLabTest\\lib\\Lib', '__classpath__', '__pyclasspath__/']
這兒就只有eclipse的工程的路徑包含了,所以當然找不到urllib啦。而在命令行下打印sys.path爲:
用jython差的lib庫少太多了,也懶得用類似sys.path.add("D:\\jython2.5.2\\Lib");一個一個加了,所以果斷放棄jython。
然後查到可以用Runtime.getRuntime().exec("python test.py");示例代碼如下:
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- public class Test5 {
- public static void main(String[] args){
- try{
- System.out.println("start");
- Process pr = Runtime.getRuntime().exec("python test.py");
- BufferedReader in = new BufferedReader(new
- InputStreamReader(pr.getInputStream()));
- String line;
- while ((line = in.readLine()) != null) {
- System.out.println(line);
- }
- in.close();
- pr.waitFor();
- System.out.println("end");
- } catch (Exception e){
- e.printStackTrace();
- }
- }
- }
test.py的文件內容爲:
- import sys
- import urllib
- print "hello"
- print sys.path
java程序運行的結果爲:
- start
- hello
- ['D:\\eclipse_jee_workspace\\ZLabTest', 'C:\\Windows\\system32\\python27.zip', 'D:\\Python27\\DLLs', 'D:\\Python27\\lib', 'D:\\Python27\\lib\\plat-win', 'D:\\Python27\\lib\\lib-tk', 'D:\\Python27', 'D:\\Python27\\lib\\site-packages']
- end
這就比較對了。但是中途還是遇到了很多問題,在Eclipse中運行上面的java程序拋出異常:
java.io.IOException: Cannot run program "python": CreateProcess error=2, ϵͳÕҲ»µ½ָ¶
at java.lang.ProcessBuilder.start(ProcessBuilder.java:460)
at java.lang.Runtime.exec(Runtime.java:593)
at java.lang.Runtime.exec(Runtime.java:431)
at java.lang.Runtime.exec(Runtime.java:328)
at com.mysrc.Test5.main(Test5.java:10)
Caused by: java.io.IOException: CreateProcess error=2, ϵͳÕҲ»µ½ָ¶
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(ProcessImpl.java:81)
at java.lang.ProcessImpl.start(ProcessImpl.java:30)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:453)
... 4 more
就是沒法調用python程序,而如果是在命令行下用javac編譯,然後java執行的話肯定是對的。怎麼才能在Eclipse裏也能正常運行了,網上查了半天,在run configurations->environment新建一個PATH,值設爲安裝的python的路徑,再運行就OK了。