Bootstrap

java调用python脚本

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Demo1 {
    public static void main(String[] args) {
        Process proc;
        try {
            // 执行py文件 无参
//            proc = Runtime.getRuntime().exec("python3 /Users/zfh/PycharmProjects/pythonProject/jsonTest.py");
            // 带参
            String[] args1 = new String[] { "python3", "main.py", String.valueOf(10), String.valueOf(20) };
            proc = Runtime.getRuntime().exec(args1);
            //用输入输出流来截取结果
            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            String line = null;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
            }
            in.close();
            proc.waitFor();
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}
;