我的问题涉及Process和Runtime,并尝试在允许主Java Swing GUI更新状态栏的同时运行它,所以这里是:
过去三个月,我一直在为我的软件开发小组开发内部开发工具。基础是它根据用户的条目接受GUI定义的一些变量。然后,它将这些编译成批处理文件,供命令行工具解释。点击“补丁”按钮后,只需让GUI运行命令行就相当有趣了,但我无法读取执行过程中可能出现的任何错误,这在很大程度上归功于我使用的事实runtimeVar.exec()的参数中的“start”。我能够找到一个常驻Java人来帮助我,但它从分支进程切换到另一个线程,在同一个线程中运行进程,阻止GUI响应其他任何事情。虽然该工具运行良好,但beta测试人员仍在抱怨,因为他们不知道它是否正常工作。
总而言之:我需要将runtimeVar.exec()分叉到另一个进程以允许状态栏更新,但这样做需要一些重大的返工,因为这是我的代码目前看来像:
private void runPatchTool(File directory, String batchName) throws Exception{
/***************************************************************************
* Method: runPatchTool
* Tab: Both
* Status: Completed, as of 1/10/12
*
* This method will read in a directory and batch file name and output
* these commands to the command line, running the batch file while reading
* in the output via the the InputStreamReader and parsing this to check
* for any errors that may have occurred during the patching.
*
**************************************************************************/
String tempOFP = null;
Boolean labType = false;
String tmpCountry = null;
int yesNo;
String s = " ";
String er = " ";
String[] query = new String[]{"cmd.exe","/c",directory.getPath() + "\\" + batchName};
// displayStatus("Running Patch_tool.exe . . . ");
ProcessBuilder pb = new ProcessBuilder(query);
pb.directory(directory);
try{
Process proc = pb.start();
InputStreamReader inStrmRd = new InputStreamReader(proc.getInputStream());
InputStreamReader erStrmRd = new InputStreamReader(proc.getErrorStream());
BufferedReader commandResult = new BufferedReader(inStrmRd);
BufferedReader errResult = new BufferedReader(erStrmRd);
String line = " ";
String erLine = " ";
try {
while ((line = commandResult.readLine()) != null) {
s += line + "\n";
}
while((erLine = errResult.readLine()) != null){
er += erLine + "\n";
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this,"Path Tool Error: " + e, "ERROR", JOptionPane.ERROR_MESSAGE);
}
// readOutputFiles();
} catch(IOException e){
JOptionPane.showMessageDialog(this,"Path Tool Error: " + e, "ERROR", JOptionPane.ERROR_MESSAGE);
}
StringBuffer erMsg = new StringBuffer("at least one assembler error has occurred");
CharSequence erMsgSeq = erMsg;
try{
PrintWriter outputFile = new PrintWriter(new FileOutputStream(directory.getPath() + "\\cmdout.txt"));
outputFile.print(s);
outputFile.close();
PrintWriter erFile = new PrintWriter(new FileOutputStream(directory.getPath() + "\\errout.txt"));
erFile.print(er);
erFile.close();
} catch(Exception ex){
System.out.println("Something happened in PrintWriter: " + ex);
}
}
我省略了更多代码,但它与手头的具体问题无关。
好的,尽管如此,有没有一种方法可以在不阻止GUI的情况下运行此进程,并在执行下一步之前等待进程结束(使用回调或其他方法)?