親測 ok 不帶參數的
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Deno2 {
public static void main(String[] args) throws IOException, InterruptedException {
String[] arguments = new String[] {"python", "/Users/lucax/Desktop/test實驗室2/b.py"};
//打開文件執行和讀取輸出字符
try {
Process process = Runtime.getRuntime().exec(arguments);
//這段代碼中的GBK是為了防止Python輸出中文時亂碼加的。
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream(),"GBK"));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
//java代碼中的process.waitFor()返回值為0表示我們調用python腳本成功,
//返回值為1表示調用python腳本失敗,這和我們通常意義上見到的0與1定義正好相反
int re = process.waitFor();
System.out.println(re);
} catch (Exception e) {
}
}
}
參考: https://blog.csdn.net/wl_Honest/article/details/84340944