28

If I start a process via Java's ProcessBuilder class, I have full access to that process's standard in, standard out, and standard error streams as Java InputStreams and OutputStreams. However, I can't find a way to seamlessly connect those streams to System.in, System.out, and System.err.

It's possible to use redirectErrorStream() to get a single InputStream that contains the subprocess's standard out and standard error, and just loop through that and send it through my standard out—but I can't find a way to do that and let the user type into the process, as he or she could if I used the C system() call.

This appears to be possible in Java SE 7 when it comes out—I'm just wondering if there's a workaround now. Bonus points if the result of isatty() in the child process carries through the redirection.

John Calsbeek
  • 35,947
  • 7
  • 94
  • 101

3 Answers3

16

You will need to copy the Process out, err, and input streams to the System versions. The easiest way to do that is using the IOUtils class from the Commons IO package. The copy method looks to be what you need. The copy method invocations will need to be in separate threads.

Here is the basic code:

// Assume you already have a processBuilder all configured and ready to go
final Process process = processBuilder.start();
new Thread(new Runnable() {public void run() {
  IOUtils.copy(process.getOutputStream(), System.out);
} } ).start();
new Thread(new Runnable() {public void run() {
  IOUtils.copy(process.getErrorStream(), System.err);
} } ).start();
new Thread(new Runnable() {public void run() {
  IOUtils.copy(System.in, process.getInputStream());
} } ).start();
John Meagher
  • 22,808
  • 14
  • 54
  • 57
  • 5
    Not quite. IOUtils.copy takes InputStream as it's first argument, so that won't work with getOutputStream. So it is IOUtils.copy(process.getInputStream(), System.out); It's a bit confusing, as getOutputStream actually pipes to input of the process. – Eelco Oct 15 '09 at 04:03
  • The structure of this looks right, but I also agree that the parameters to IOUtils seems backwords. the process.getInputStream() is what should be piped to stdout (yeah, it sounds backwards). So while the structure of this looks good, the calls to IOUtils look wrong and now have me a bit confused... – titania424 Apr 07 '11 at 19:52
  • 1
    Wrong. This just **pipes** data between parent's and child's input / output streams, does not let it **inherit** them. For example even if the Java command was started on a terminal (C isatty() == true, or System.console() != null) the child will not see itself as being run in a terminal, because its input / output streams are pipes. This is important because some (most) programs behave differently when they are run in a terminal. Java 7's ProcessBuilder.Redirect.INHERIT works correctly in this regard. – Tobia Dec 22 '14 at 09:30
12

A variation on John's answer that compiles and doesn't require you to use Commons IO:

private static void pipeOutput(Process process) {
    pipe(process.getErrorStream(), System.err);
    pipe(process.getInputStream(), System.out);
}

private static void pipe(final InputStream src, final PrintStream dest) {
    new Thread(new Runnable() {
        public void run() {
            try {
                byte[] buffer = new byte[1024];
                for (int n = 0; n != -1; n = src.read(buffer)) {
                    dest.write(buffer, 0, n);
                }
            } catch (IOException e) { // just exit
            }
        }
    }).start();
}
John Calsbeek
  • 35,947
  • 7
  • 94
  • 101
Eelco
  • 1,718
  • 22
  • 16
2

For System.in use the following pipein() instead of pipe()

pipein(System.in, p.getOutputStream());

Implementation:

private static void pipein(final InputStream src, final OutputStream dest) {

    new Thread(new Runnable() {
        public void run() {
            try {
               int ret = -1;
               while ((ret = System.in.read()) != -1) {
                  dest.write(ret);
                  dest.flush();
               }
            } catch (IOException e) { // just exit
            }
        }
    }).start();

}
DarthJDG
  • 16,511
  • 11
  • 49
  • 56
claude
  • 21
  • 1