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

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐