Java进程线程问题
我的问题涉及进程和运行时,并尝试运行它,同时允许主 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 的情况下运行此进程,并在执行下一步之前等待进程结束(使用回调或其他方法)?
My problem involves Process and Runtime, and attempting to run this while allowing a main Java Swing GUI to update a status bar, so here goes:
I've been working on an internal development tool for my software development group for the past three months. The basics are that it take in some variables defined by the GUI, based upon entries by the user. Then, it compiles these into a batch file for the command line tool to interpret. Just getting the GUI to run the command line once you hit the "patch" button was fairly interesting, but I was unable to read in any errors that may have arisen during the execution, due in no small part to the fact that I was using "start" in the arguments for runtimeVar.exec(). I was able to get a resident Java guy to help me out, but it switched from forking the process to another thread to running the process in the same thread, blocking the GUI from responding to anything else. While the tool runs perfectly, the beta testers are complaining because they don't know if it's working or not.
To make a long story short: I need to fork the runtimeVar.exec() to another process in order to allow the status bar to update, but doing this will require some major rework, since this is what my code currently looks like:
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);
}
}
There is more code that I have omitted, but it does not pertain to the specific issue at hand.
Alright, with all that being said, is there a way to run this process while not blocking the GUI, and yet waiting for the process to end (using a callback or some other method) before executing the next step?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论