java 利用Runtime.getRuntime().exec()调用python脚本并传参

时间:2021-02-22 20:30:34
 
//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 sysprint sys.argv[0]print sys.argv[1]print sys.argv[2]print sys.argv[3]print sys.argv[4]