//TODO:执行python脚本 System.out.println("start python"); //需传入的参数 String a = "aaa", b = "bbb", c = "ccc", d = "ddd"; System.out.println("start;;;" + a); //设置命令行传入参数 String[] args = new String[] { "python", "C:\\Users\\Desktop\\test1.py", a, b, c, d }; Process pr = Runtime.getRuntime().exec(args); //TODO:该方法只能传递字符串 // Process pr = Runtime.getRuntime().exec("python C:\\Users\\Desktop\\test1.py C:\\Users\\hpp\\Desktop\\test1.mp4"); BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream())); String line; while ((line = in.readLine()) != null) { // line = decodeUnicode(line); System.out.println(line); } in.close(); pr.waitFor(); System.out.println("end");
test1.py (测试是否能取到参数)
import sys print sys.argv[0] print sys.argv[1] print sys.argv[2] print sys.argv[3] print sys.argv[4]
本文介绍了一个使用Java程序调用Python脚本的例子,展示了如何通过命令行参数传递方式实现跨语言交互,并获取Python脚本的输出结果。
8014





