java程序调用python程序输出python结果(有参)

java程序调用python程序输出python结果(有参)_第1张图片

java代码

@Test
    public void contextLoads() throws IOException, InterruptedException {
        //需传入的参数
        String a = "aaa", b = "bbb", c = "ccc", d = "ddd";
        //设置命令行传入参数
        String[] args = new String[] { "python", "C:\\Users\\tianyong\\Desktop\\test.py", a, b, c, d };
        Process pr = Runtime.getRuntime().exec(args);

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

import sys
print (sys.argv[0])
for i in range(1, len(sys.argv)):
    print (i, sys.argv[i])

 

你可能感兴趣的:(corejava)