0

This is my current code:

ProcessBuilder builder = new ProcessBuilder(
                        "cmd.exe", "/c", cmd1 + " && " + cmd2 + " && " + cmd3 + " && " + cmd4 + " && " + cmd5 + " && " + cmd6
                );
                builder.directory(new File(workspace.toString()));
                builder.redirectErrorStream(true);
                Process p = builder.start();
                BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
                String line;
                while(true) {
                    line = r.readLine();
                    if(line == null) {
                        break;
                    }
                    //textPanel.appendText(line);
                    System.err.println(line);
                    textPanel.appendText(line);

                }

How can I check for if all commands were executed successfully, and after cmd6 is done, it sends a sysout message?

I have not been able to figure out how to do this...

Jonathan
  • 685
  • 1
  • 10
  • 30
  • 1
    This will simply lauch one command, so only one query I believe (can't test). So it will fail or not. To see when this fails, you will need to run each command separatly. – AxelH Jan 24 '17 at 10:38
  • 1
    @AxelH But the commands will execute after the previous one is finished successfully, right? Or did I mis-read the docs?? If that's the case, I could just send a `p.waitFor(); // message` and that should send a message when all commands have been completed. Correct? – Jonathan Jan 24 '17 at 10:57
  • You can simply test ;) I don't have an environnement ready but you are simply generating one process to execute the cmd query. So this will be manage by this process only. – AxelH Jan 24 '17 at 11:09
  • From the small snippet of code, you're either going to block the EDT (stopping it from updating the UI) or are violating the single thread rules of Swing – MadProgrammer Jan 24 '17 at 11:09
  • @MadProgrammer yeah so once I tried it, the software was just standing still until something failed. How can I prevent this? – Jonathan Jan 24 '17 at 12:36
  • Use s SwingWorker – MadProgrammer Jan 24 '17 at 18:39
  • @MadProgrammer so from what I can gather from other threads and the docs, I just need another class that extends SwingWorker and use the same code? And by same code I mean the processbuilder etc. – Jonathan Jan 25 '17 at 07:50
  • You need to wrap the call to `ProcessBuilder` in the `SwingWorker`, but essentially, yes. For [example](http://stackoverflow.com/questions/30797851/update-jlabel-content-from-the-output-of-shell-script/30798684#30798684), [example](http://stackoverflow.com/questions/34858405/how-can-i-make-this-method-update-the-gui-within-my-loop/34864911#34864911) and [example](http://stackoverflow.com/questions/18475942/java-terminal-only-printing-output-from-first-command/18476428#18476428) – MadProgrammer Jan 25 '17 at 08:12
  • @MadProgrammer this will be a interesting read! Hopeful that I'll learn new stuff, thanks! :) – Jonathan Jan 25 '17 at 08:14

0 Answers0