java calls python script, passing in parameters

1. java code

public class Cmd {

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

		try {
			System.out.println("start");
              //Runtime.getRuntime().exec("python script path parameter 1 parameter 2 parameter n");
              // does not support incoming parameters
			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 code

import sys 
import urllib 
print (sys.argv[0]) #Parameter[0] is the script path
print (sys.argv[1]) #Parameter[1] is the incoming parameter 1

 

3. Results

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326166297&siteId=291194637