java调用python并传递参数

python程序:testpj.py

#coding:utf-8

import sys

print sys.argv[1]

java程序:Testpj.java

import java.io.BufferedReader;

import java.io.InputStreamReader;

public class Testpj{

public static void main(String[] args)throws Exception {

Process pr = Runtime.getRuntime().exec("python testpj.py 123");

//获取python文件运行后的输出

BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream()));

String line;

while ((line = in.readLine()) != null) {

System.out.println(line);

     }

in.close();

pr.waitFor();

System.out.println("sucecss!");

   }

}

运行结果:

MacBook-Air:Downloads huangyong$ java Testpj

123

sucecss!

文件地址:

github.com/Inspiring26/wow/tree/master/java调用python

你可能感兴趣的:(java调用python并传递参数)