0

Using Java's ProcessBuilder, I can run an external script, and redirect its output into my GUI.

        Process proc = pb.start();
        BufferedReader bri = new BufferedReader(new InputStreamReader(proc.getInputStream()));
        String line = "";
        while (proc.isAlive())
        {
          // bri may be empty or incomplete.
          while ((line = bri.readLine()) != null)
          {
            textArea.appendText(line);
          }
        }

Now, the script I am running also calls other scripts and processes. Two of these, that should be captured, are currently displayed in their own xterm windows. Is it possible to also capture these outputs, and display in a similar manner?.

AerusDar
  • 210
  • 6
  • 22
  • To start with, your `while` loop is dangerous, particularly if you are trying to update the UI, either you'll block the EDT or are violating the single thread rules of Swing. Consider having a look at this [example](http://stackoverflow.com/questions/15801069/printing-a-java-inputstream-from-a-process/15801490#15801490), which updates a `JTextArea` with the output of a given process – MadProgrammer Jun 18 '14 at 02:38
  • This section of code is being run in its own thread, I didn't see the need to post the threading code in this question. Thank you though! – AerusDar Jun 18 '14 at 02:40
  • So, instead, you're just violating the single thread rules of Swing instead – MadProgrammer Jun 18 '14 at 02:44
  • Sure looks that way. I'm taking a look through your linked answer which can help clean up that issue. – AerusDar Jun 18 '14 at 02:53
  • The overall answer depends, it depends on how the system chains each process's stdout, it depends if any process executes in a non-blocking manner, allowing the other process to continue. In theory, it should be possible – MadProgrammer Jun 18 '14 at 02:56

1 Answers1

0

If these outputs are being managed by your source script, I think it will work. Just as an advice, take a look in this article: When Runtime.exec() won't It is extremammly important to read it to learn how to work with external processes correctly.

davidbuzatto
  • 9,207
  • 1
  • 43
  • 50