第一种方法,使用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");
Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐