import sys
def main(a):
print(url)
print(sys.argv)
print(len(sys.argv))
if __name__ == '__main__':
for i in range(1, len(sys.argv)):
url = sys.argv[i]
main(url)
java代码
package python;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import org.python.antlr.PythonParser.test_return;
import org.python.util.PythonInterpreter;
import jnr.ffi.Struct.int16_t;
public class java_python {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入需要传递给python的参数");
String url = input.nextLine();
String a = "jia";
// System.out.println(integers);
// String url="http://blog.csdn.net/thorny_v/article/details/61417386";
String[] cmds = new String[]{"python", "C:\\Users\\张家豪\\Desktop\\test.py",url,a};
// System.out.println(cmds);
System.out.println("调用python程序");
Process pcs;
try {
pcs = Runtime.getRuntime().exec(cmds);
pcs.waitFor();
// 定义Python脚本的返回值
String result = null;
// 获取CMD的返回流
BufferedInputStream in = new BufferedInputStream(pcs.getInputStream());
// 字符流转换字节流
BufferedReader br = new BufferedReader(new InputStreamReader(in));
// 这里也可以输出文本日志
String lineStr = null;
// System.out.println(br.readLine());
while ((lineStr = br.readLine()) != null) {
result = lineStr;
// System.out.println(br.readLine());
System.out.println(result);
}
//
// 关闭输入流
br.close();
in.close();
} catch (IOException | InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}