我有一个Java脚本,它使用Process builder启动一个新线程来执行python脚本。下面的代码当前从python获取输出并将其显示在Java运行输出和JTextArea中。但是,一旦py脚本运行完毕,它只会批量执行。有没有办法让输出显示为从py脚本中写出来的?感谢!!!!
public void launchPythonScript() {
try {
ProcessBuilder py = new ProcessBuilder("cmd", "/C", "PythonScriptLocation (C:\\....)",""+Directory(variable needed for py script));
Process launch = py.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(launch.getInputStream()));
String readLine;
StringBuilder JavaOutput = new StringBuilder();
while((readLine = reader.readLine()) != null){
JavaOutput.append(readLine).append(System.lineSeparator());
frame2.consoleOutput.setText(JavaOutput.toString());
System.out.println(readLine);
}
} catch (IOException ex) { Logger.getLogger(Frame1.class.getName()).log(Level.SEVERE, null, ex);}
}
答案 0 :(得分:0)
进程InputStream应该在脚本运行时为您提供数据。
你正在阅读一行,所以如果你的python脚本在一行中发送所有数据,那么你只能在最后看到输出。
尝试将输出数据分成多行。