0

I am a newbie to Java programming. I have a program that executes a system command using Runtime exec Process and I want it while the Process is running, the JProgressBar indicator is updating. How can I do this programmatically?

My code is something like:

...
try {
    Process p = Runtime.getRuntime().exec(command_goes_here);

    int error = p.waitFor();

    // Then the JProgressBar???

    System.out.println((error == 0 ? "Success" : "Failed"));
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Humble Potato II
  • 161
  • 1
  • 1
  • 12
  • I found a duplicate which pretty much gives you the answer. I suspect that you were using the wrong terms for your Google search since searching on Swing JProgressBar process gives quick hits to many similar solutions. – Hovercraft Full Of Eels Jan 08 '17 at 04:09
  • @HovercraftFullOfEels Sorry for this late reply. I am currently trying to implement the answer to the "duplicate question". The answer uses the `ProcessBuilder` which is in my case is different so I'm having a hard time here, and I don't understand the other code used (cause there isn't any comment). I'm really sorry. – Humble Potato II Jan 08 '17 at 04:47
  • In fact you should be using a ProcessBuilder instead of `Runtime.getRuntime().exec(...)` to get your Process. Read the API on ProcessBuilder to see how to use it. Next, you should gain an understanding of Swing thread issues. Please read [Lesson: Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) (yes, all of it) to gain this understanding. – Hovercraft Full Of Eels Jan 08 '17 at 04:48
  • @HovercraftFullOfEels My bad for asking such noobie question... I'll work on it. Thanks! Though I got the Runtime exec `Process` code working perfect in my program, I'll also try the `ProcessBuilder`. – Humble Potato II Jan 08 '17 at 04:53
  • @HovercraftFullOfEels Does `ProcessBuilder` requires an external file like **.BAT** **.SH**? Cause I'm getting an error "The system cannot find the file specified". – Humble Potato II Jan 08 '17 at 05:16
  • I don't understand your question. A ProcessBuilder requires a command list or array, one that is called as an external process on the operating system. – Hovercraft Full Of Eels Jan 08 '17 at 05:21

0 Answers0