我的程序要求我运行一个 .bat 文件,该文件将编译 java 源代码。这运行良好,但是我正在寻找一种解决方案,它将获取 compile.bat 的输出(和可能的错误)并将其添加到 GUI 上的文本窗格中。我有以下代码,但是当执行该过程时,不会在窗格中打印任何内容,也不会出现任何错误。
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) {
}
}
生成调试窗口
private static JTextPane outputPane;
public static void writeToPane(String i){
outputPane.setText(outputPane.getText() + i + "\r\n");
}