简体   繁体   中英

Java ProcessBuilder: capture output from scripts run from scripts run from Java

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?.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM