public static void main(String[] args) { // TODO Auto-generated method stub Process proc; try { String[] args1 = new String[] { "python.exe", "python.py","test"};//python.exe處為你系統中python的安裝位置;python.py為想要執行的python文件位置;test為想要傳的參數 //proc = Runtime.getRuntime().exec("python.exe python.py ");// 執行py文件 不傳參數 proc=Runtime.getRuntime().exec(args1); //用輸入輸出流來截取結果 BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream())); String line = null; while ((line = in.readLine()) != null) { System.out.println(line); } in.close(); proc.waitFor(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } }
python.py文件
import sys print(sys.argv[0]) //輸出為python文件的路徑 print(sys.argv[1]) //輸出為傳入的第一個參數test
