How might I get the output from a CMD process to display in my GUI? This is the code I’m using to run the process:
try { String line; Process p = Runtime.getRuntime().exec('cmd /c \'e:\\folder\\someCommands.cmd\''); BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream())); while ((line = input.readLine()) != null) { System.out.println(line); } input.close(); } catch (Exception err) { err.printStackTrace(); }
I’ve tried doing this:
jLabel1.setText(line);
…but the GUI is completely locked up while the process is running, so nothing updates until the very end, which isn’t very useful. Other than that the CMD works fine. I just want to display the output in real-time.
Did you repaint() after setting the text of the label?
Anyway, you should generally be hesitant to execute a long operation on the GUI event thread. Look into using a
SwingWorkerinstead.