Take the 2-minute tour ×

in a bash script, if I execute an external program (i.e. 'bash') that is executed "inline". I mean that the process is not spanned in background and the stdin/stdout/stderr of the child process coincide with the one of the bash script itself.

So if my bash script contains


what happens is that the user can run my script and when bash is executed he will be able to type commands in the standard input of bash and see the results of the command on stdout/stderr.

This is what I mean that the child process is run "inline".

In java the process is spanned in background, so the Process.getOutputStream()/Process.getInputStream()/Process.getErrorStream are not "inline" with System.in/System.out/System.err.

What I want to do in my java program is to reproduce the behavior of what happens when I execute a process as in the above example of the bash script.

After some google searches I come out with this

        InterruptedException {
    String[] cmdarray = {"bash"};
    Process proc = Runtime.getRuntime().exec(cmdarray);

    StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(),

    StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(),

    StreamGobbler inputGobbler = new StreamGobbler(System.in,


    int exitVal = proc.waitFor();
    errorGobbler.join(); // Handle condition where the
    outputGobbler.join(); // process ends before the threads finish

class StreamGobbler extends Thread {
    InputStream is;
    OutputStream os;

    StreamGobbler(InputStream is, OutputStream os) {
        this.is = is;
        this.os = os;

    public void run() {
        try {
            int c;
            while ((c = is.read()) != -1) {
        } catch (IOException x) {
            throw new RuntimeException(x);

But hey, there are 3 threads! plus the thread spanned by executing the process!

There must be a better way. Something like:

Runtime.execForeground("bash", System.in, System.out, System.err);

or just:


which executes the process "inline" as it works with many script languages.

Maybe another possibility is to use nonblocking I/O to have the stdin/stdout/stderr copied to System.in/out/err in a single thread? Any example?

share|improve this question
Don't worry about the threads. Your only alternative is to find something based on Java NIO, but the offering there is scant, very much due to the fact that the world doesn't worry about the extra treads -- it's the Java way for handling this. NIO is an order of magnitude more cumbersome to code against, and the benefits are negligible. –  Marko Topolnik Apr 19 '12 at 9:09
I think that java API for process execution is wrong. I would expect to be able to do something like: ProcessFork pf = new Process(...) pf.setStandardInput(System.in); pf.setStandardOutput(System.out); pf.setStandardError(System.err); pf.setForeground(); Process p = pf.execute(); or something like that –  Luigi R. Viggiano Apr 19 '12 at 9:59
True, this is obviously not a feature that JDK supports out of the box. There's a solid chance for a library out there doing pretty much the same thing that you outlined here, though. –  Marko Topolnik Apr 19 '12 at 10:02
There is this library commons.apache.org/exec; don't know if it solves my problem, but I'm having a look right now. –  Luigi R. Viggiano Apr 19 '12 at 10:58
Update: commons-exec is the same stuff which is included in ant for the exec task, and it does the same thread stuff as I explained above, in a much more complicated way. Not for me. –  Luigi R. Viggiano Apr 19 '12 at 11:05
show 5 more comments

Your Answer



Browse other questions tagged or ask your own question.