您的位置:首页 > 编程语言 > Python开发

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

2018-02-24 16:23 921 查看
//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 (测试是否能取到参数)



[python] view
plain copy

import sys  

print sys.argv[0]  

print sys.argv[1]  

print sys.argv[2]  

print sys.argv[3]  

print sys.argv[4] 

原文地址:http://blog.csdn.net/hpp1314520/article/details/72854011
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: