0

I'm currently creating a Process to run a script from my jar file using the following code:

Process p = Runtime.getRuntime().exec("...");

BufferedReader stdInput = new BufferedReader(new
        InputStreamReader(p.getInputStream()));

// Read the output from the command
String s;
while ((s = stdInput.readLine()) != null) {
    if(s.equals("success")) {
        return true;
    }
}

However when I press a button in my GUI which calls this code, the program freezes until the process output returns success. How could I stop the program from freezing? I plan on showing a GUI to display the time taken (live) while the process is being executed.

madcrazydrumma
  • 1,847
  • 3
  • 20
  • 38
  • Of course, it's blocking in `readLine()`. You must do all that interaction with the process in a separate thread. – Jim Garrison Apr 06 '18 at 18:47
  • @JimGarrison would I have to create an extension of thread in a new class or is there a way to spawn a thread for just this purpose? Also I saw somewhere I can use `SwingWorker`. What would be better? – madcrazydrumma Apr 06 '18 at 19:10

0 Answers0