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?