gpt4 book ai didi

java - 如何从 .bat 进程获取输出以显示在 JTextPane 中?

转载 作者:行者123 更新时间:2023-11-29 05:57:17 25 4
gpt4 key购买 nike

我的程序要求我运行一个 .bat 文件来编译 java 源代码。这运行良好,但是我正在寻找一种解决方案,它将获取 compile.bat 的输出(和可能的错误)并将其添加到 GUI 上的文本 Pane 中。我有以下代码,但是当执行该过程时,不会向 Pane 打印任何内容,也没有任何错误。

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");
}

最佳答案

使用:

Runtime.getRuntime().exec( "cmd.exe /C " + file.getAbsolutePath() + "\\compile.bat" );

关于java - 如何从 .bat 进程获取输出以显示在 JTextPane 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11588556/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com