Window环境下,Java调用Python

public static void main(String[] args) throws IOException, InterruptedException {
String workDir = "E:/python_workspace/Python/machine_learning/forecasting";
String[] envp = new String[]{"WORKDIR=" + workDir};
Process exec = Runtime.getRuntime().exec("python ./run.py /c dir", envp, new File(workDir));
BufferedReader reader = new BufferedReader(new InputStreamReader(exec.getInputStream(),"GBK"));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
   //waitFor()方法目的是为了保证CMD命令执行完毕
int ec = exec.waitFor();
System.out.println("Process exited with code " + ec);
reader.close();
}
posted @ 2024-11-21 16:14  熊猫不唱歌  阅读(0)  评论(0编辑  收藏  举报