java 呼叫 python程式碼
阿新 • • 發佈:2022-03-26
package test01; import sun.applet.Main; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * @Author chun * @date 2022/3/26 18:23 * @Des **/ public class TestRuntime { public static void main(String[] args) { try { // String command = "cmd /c python 【python檔案位置 】 -t 【引數】";// String command = "cmd /c python C://Users//liyujiao//Desktop//FAPlus//auto//ymxia1test//testSendData.py -t 1"; String command = "cmd /c python C://Users//liyujiao//Desktop//FAPlus//auto//ymxia1test//test04.py -t 2"; Runtime rt = Runtime.getRuntime(); Process pr = rt.exec(command); System.out.println("---執行結束-1-"); //python程式執行結束後才會繼續向下走 pr.waitFor(); // 讀取python的print語句列印資料 BufferedReader b = new BufferedReader(new InputStreamReader(pr.getInputStream())); String line = ""; StringBuilder response = new StringBuilder(); while ((line = b.readLine()) != null) { response.append(line); } //在java控制檯 打印出python的print語句資料 String response_data = response.toString(); System.out.println("response_data="+response_data); b.close(); System.out.println("---執行結束-2-"); } catch (Exception e) { e.printStackTrace(); } } }