Home  >  Article  >  Backend Development  >  Java calls Python

Java calls Python

巴扎黑
巴扎黑Original
2016-12-09 10:31:111210browse

今天遇到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为:

Txt代码  

['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的文件内容为:

Python代码  

import sys  
import urllib  
print "hello"  
print sys.path

 java程序运行的结果为:

Txt代码  

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.7e51f00a783d7eb8f68358439dee7daf(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了。


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn