我的程序要求我运行一个.bat文件,它将编译java源代码。这运行良好,但我正在寻找一个解决方案,它将获得compile.bat的输出(和可能的错误)并将其添加到GUI上的文本窗格中。我有下面的代码,但是当执行过程发生时不打印任何内容到窗格并且没有任何错误。如何从.bat进程获取输出以显示在JTextPane中?
GenerationDebugWindow.main(null);
Process process = rut.exec(new String[] {file.getAbsolutePath() + "\\compile.bat"});
Scanner input = new Scanner(process.getInputStream());
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader reader = new BufferedReader(isr);
String line;
int exit = -1;
while ((line = reader.readLine()) != null) {
// Outputs your process execution
try {
exit = process.exitValue();
GenerationDebugWindow.writeToPane(line);
System.out.println(line);
if (exit == 0) {
GenerationDebugWindow.writeToPane("Compilation Finished!");
if(new File(file + "/mod_" + WindowMain.modName.getText()).exists()){
GenerationDebugWindow.writeToPane("Compilation May Have Experienced Errors.");
}
}
} catch (IllegalThreadStateException t) {
}
}
GenerationDebugWindow
private static JTextPane outputPane;
public static void writeToPane(String i){
outputPane.setText(outputPane.getText() + i + "\r\n");
}
你_read_为'Process.exitValue的javadoc()'将来电转接到您的代码,或只是坚持catch块在你的代码,因为IDE叫你过吗? – jtahlborn 2012-07-21 00:41:50