java调用python脚本,传入参数

1. java代码

public class Cmd {

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

		try {
			System.out.println("start");
              //Runtime.getRuntime().exec("python 脚本路径 参数1 参数2 参数n");
              //不支持传入形参
			Process pr = Runtime.getRuntime().exec("python C:\\Users\\54225\\Desktop\\test1.py sample-debug.apk");
                        
			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("end");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

2.python代码

import sys 
import urllib 
print (sys.argv[0])    #参数[0] 是脚本路径
print (sys.argv[1])    #参数[1] 是传入参数1

3.结果

start
C:\Users\54225\Desktop\test1.py
sample-debug.apk
end

猜你喜欢

转载自542255641.iteye.com/blog/2399869