JAVA 调用python脚本的方法

来源:互联网 发布:大淘客cms有什么用 编辑:程序博客网 时间:2024/06/04 18:56

第一种方法,使用Jython

PythonInterpreter interpreter = new PythonInterpreter();InputStream filePy = new FileInputStream("/root/jni/py/hello.py");interpreter.execfile(filePy);filePy.close();

使用到PythonInterpreter执行python脚本

第二种方法,使用Process进程执行

 String[] arg = new String[]{"python","/root/jni/py/hello.py","1","2","3","4"};        Process process = Runtime.getRuntime().exec(arg);        BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));        String line;        while ((line = in.readLine()) != null) {          System.out.println(line);        }        in.close();        process.waitFor();        System.out.println("end");
原创粉丝点击