运行 个批处理文件 Java
Running batch files in Java
我正在使用进程构建器来 运行 批处理文件,并且我正在尝试让进度条和文本区域在每个批处理文件完成后进行更新。
问题是,直到所有三个批处理文件都完全完成后,进度条和文本区域才会更新,然后它只显示进度条和文本区域的最后更新(我猜它正在做他们一次全部,最后一个 progressbar.setValue 超越先前的值)
这里是 运行 三个批处理文件之一的方法之一:
public void runBatch1() {
try {
ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", "cd \"C:\batchfiles\" && batch1.bat");
builder.redirectErrorStream(true);
Process p = builder.start();
BufferedReader r = new BufferedReader(new InputSteamReader(p.getInputStream()));
String line;
while (true) {
line = r.readLine();
if (line == null) {
break;
}
System.out.println(line);
jTextArea1.setText("Batch one finished.");
jProgressBar1.setValue(33);
}
} catch (Exception e) {
e.printStackTrace();
}
}
我认为您可能只需要切换到事件分发线程:
progressbar in a thread does not update its UI until the work was done in the main thread
我正在使用进程构建器来 运行 批处理文件,并且我正在尝试让进度条和文本区域在每个批处理文件完成后进行更新。
问题是,直到所有三个批处理文件都完全完成后,进度条和文本区域才会更新,然后它只显示进度条和文本区域的最后更新(我猜它正在做他们一次全部,最后一个 progressbar.setValue 超越先前的值)
这里是 运行 三个批处理文件之一的方法之一:
public void runBatch1() {
try {
ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", "cd \"C:\batchfiles\" && batch1.bat");
builder.redirectErrorStream(true);
Process p = builder.start();
BufferedReader r = new BufferedReader(new InputSteamReader(p.getInputStream()));
String line;
while (true) {
line = r.readLine();
if (line == null) {
break;
}
System.out.println(line);
jTextArea1.setText("Batch one finished.");
jProgressBar1.setValue(33);
}
} catch (Exception e) {
e.printStackTrace();
}
}
我认为您可能只需要切换到事件分发线程: progressbar in a thread does not update its UI until the work was done in the main thread