Java执行本地Python脚本

1:add.py

a = 100
b = 200
print (a + b)

2:测试代码

Process proc = Runtime.getRuntime().exec("python D:\\project\\python\\add.py");

            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            String line = null;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
            }
            in.close();
            proc.waitFor();

 

也可以使用jython-standalone工具包实现,详情见:https://www.jython.org/index

 

posted @ 2021-01-14 12:09  yshy  阅读(247)  评论(0编辑  收藏  举报