Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

process

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.

like image 497
Denilson Sá Maia Avatar asked May 10 '12 19:05

Denilson Sá Maia


3 Answers

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)
like image 88
henry Avatar answered Apr 03 '23 02:04

henry


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();
    }
like image 44
user3473636 Avatar answered Apr 03 '23 02:04

user3473636


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.

like image 26
asudell Avatar answered Apr 03 '23 01:04

asudell