1

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?

Eric Wilson
  • 57,719
  • 77
  • 200
  • 270
ThomSirveaux
  • 141
  • 1
  • 4
  • 2
    This is what SwingWorker is for: http://docs.oracle.com/javase/6/docs/api/javax/swing/SwingWorker.html –  Feb 21 '12 at 19:12
  • 1
    @a_horse_with_no_name hmmm I have very wrong experiences with `SwingWorker` and `Process / ProcessBuider` together, fo me works without any lacks from `Executor`, `Timer` or `Runnable#Thread`, please read http://stackoverflow.com/questions/7053865/cant-get-arrayindexoutofboundsexception-from-future-and-swingworker-if-threa – mKorbel Feb 21 '12 at 19:21
  • @a_horse_with_no_name works but (maybe vacuum in my head) I'm not to be able load Process.getErrorStream() correctly, I simulating that nTimes ...., ok please leaving this debate :-) – mKorbel Feb 21 '12 at 19:29
  • @mKorbel: Problems with Process.getErrorStream() have nothing to do with using SwingWorker in a Swing GUI –  Feb 21 '12 at 19:46
  • Alright, I'm reading up on SwingWorker, but I'm wondering if ProcessBuilder would also work, per @mKorbel. Anything that would just run the process and wait until it finishes, without blocking the GUI would work just fine. (Maybe I should have put this in my original question >.<) – ThomSirveaux Feb 21 '12 at 21:27
  • @ThomSirveaux basically you can use that, could be my error – mKorbel Feb 21 '12 at 21:37
  • Also, just another note, please remember to close your streams. Oh, and never cross them, that would be bad too. – deraj Feb 21 '12 at 23:48

0 Answers0