Denilson Sá Maia
Denilson Sá Maia

Reputation: 49407

How to redirect child process stdout/stderr to the main process stdout/stderr in Java?

In other languages (like bash and Python), when we spawn a child process, this new process will inherit the stdout and stderr from the parent. This means that any output from the child process will be printed to the terminal as well as the output from the parent.

How can we achieve the same behavior in Java?

My first try was:

proc = Runtime.getRuntime().exec(cmd);

But it won't work. Based on this answer and this answer, I've replaced the code with:

ProcessBuilder pb = new ProcessBuilder(cmd);
pb.redirectOutput(System.out);
pb.redirectError(System.err);

But this doesn't even compile, as the arguments are incompatible with the expected method parameters.

Upvotes: 11

Views: 9428

Answers (3)

user3473636
user3473636

Reputation: 31

This isn't too bad. I initially said "easy", then I realized I'd had to code the InputStreamConsumer:

public static class InputStreamConsumer extends Thread {

    private InputStream is;

    public InputStreamConsumer(InputStream is) {
        this.is = is;
    }

    @Override
    public void run() {

        try {
            int value = -1;
            while ((value = is.read()) != -1) {
                System.out.print((char)value);
            }
        } catch (IOException exp) {
            exp.printStackTrace();
        }
    }
}

private void captureOutput(Process p) {

    InputStreamConsumer stdout;
    InputStreamConsumer errout;

    errout = new InputStreamConsumer(p.getErrorStream());
    stdout = new InputStreamConsumer(p.getInputStream());
    errout.start();
    stdout.start();
}

....running inside of something like ...

    ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/D", "/C", myCommand, parm, parm...);
    try {
        System.out.println("Start "+myCommand);
        Process myProcess = pb.start();
        captureOutput(myProcess);

        int returnCode = myProcess.waitFor();
        System.out.println("myProcess: return code : "+returnCode);
    }
    catch (IOException e) {
        e.printStackTrace();
    }

Upvotes: 3

henry
henry

Reputation: 6096

You need to read the output and error streams of the process you've created and write the output to where you want it to go (in your case System.out and System.err).

Edit. The above answer is correct for java < 7. As of 7 from the javadoc it looks to be possible to instead call

processBuilder.redirectOutput(Redirect.INHERIT)

Upvotes: 18

asudell
asudell

Reputation: 101

It's certainly possible. The Ant Java task does this when fork is set true. I don't remember all the details, but on a quick look it appears that most of the magic is in PumpStreamHandler. You might try finding some inspiration from that.

Upvotes: 1

Related Questions