Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start external executable from Java code with streams redirection

I need to start external executable in such way that user can interact with program that was just started.

For example in OpenSuse Linux there is a package manager - Zypper. You can start zypper in command mode and give commands like install, update, remove, etc. to it.

I would like to run it from Java code in a way user could interact with it: input commands and see output and errors of the program he started.

Here is a Java code I tried to use:

public static void main(String[] args) throws IOException, InterruptedException {
    Process proc = java.lang.Runtime.getRuntime().exec("zypper shell");

    InputStream stderr = proc.getInputStream();
    InputStreamReader isr = new InputStreamReader(stderr);
    BufferedReader br = new BufferedReader(isr);
    String line = null;
    char ch;

    while ( (ch = (char)br.read()) != -1)
        System.out.print(ch);

    int exitVal = proc.waitFor();
    System.out.println("Process exitValue: " + exitVal);
}

But unfortunately I can only see it's output:

zypper>

but no matter what I write, my input doesn't affect program that was started. How can I do what want to?

like image 264
Ivan Mushketyk Avatar asked Nov 20 '10 10:11

Ivan Mushketyk


1 Answers

You need to get an output stream in order to write to the process:

OutputStream out = proc.getOuptutStream();

This output stream is piped into the standard input stream of the process, so you can just write to it (perhaps you want to wrap it in a PrintWriter first) and the data will be sent to the process' stdin.

Note that it might also be convenient to get the error stream (proc.getErrorStream) in order to read any error output that the process writes to its stderr.

API reference:

  • http://download.oracle.com/javase/6/docs/api/java/lang/Process.html
like image 185
Grodriguez Avatar answered Oct 06 '22 00:10

Grodriguez