3 Replies Latest reply: Sep 1, 2010 6:06 PM by EJP RSS

    Interacting with another process

    843790
      I'm trying to launch and then interact with another process. I can already launch and then see the output of a process:
      public static void main(String[] args) throws IOException, InterruptedException {
           Process p = Runtime.getRuntime().exec("ls");
           InputStream input = p.getInputStream();
           p.waitFor();
           Scanner s = new Scanner(input);
           while (s.hasNextLine()) {
                System.out.println(s.nextLine());
           }
      }
      I think I could figure out how to interact with a process (such as a shell or ftp connection) with considerable trial and error, but can anyone point me to a tutorial or small example on the "right" way to do it?

      Thanks,
        • 1. Re: Interacting with another process
          3004
          You probably want to start here: [http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html]

          Note, however, that the article pre-dates ProcessBuilder, which is the preferred replacement for Runtime.exec. You might be able to do what the article recommends somewhat more simply with ProcessBuilder (or maybe not). The same principles apply either way.

          The short version is: you need separate threads for the other process's I/O: at least one for stdin and one combined stdout/stderr, and possibly 3 separate ones for each of stdin, stdout, stderr.

          Also, you'll need to know that process's behavior: What it expects for stdin, how it responds, what those responses mean, etc. Some programs won't read anything from stdin. Others have a somewhat asynchronous console model. Others are simply request/response. If the process outputs control characters to manipulate the appearance of the console, you'll have to deal with that.
          • 2. Re: Interacting with another process
            843790
            Okay, this seems to work as an interactive bash:
            package orego.experiment;
            
            import java.io.*;
            import java.util.*;
            
            public class Launch {
            
                 public static void main(String[] args) throws IOException, InterruptedException {
                      final Process p = new ProcessBuilder("/bin/bash").start();
                      PrintWriter output = new PrintWriter(p.getOutputStream());
                      Runnable listener = new Runnable() {
                           public void run() {
                                Scanner fromProgram = new Scanner(p.getInputStream());
                                while (fromProgram.hasNextLine()) {
                                     System.out.println(fromProgram.nextLine());
                                }
                           }
                      };
                      new Thread(listener).start();
                      Scanner in = new Scanner(System.in);
                      while (true) {
                           String command = in.nextLine();
                           output.println(command);
                           output.flush();
                      }
                 }
            
            }
            Thanks for your help.
            • 3. Re: Interacting with another process
              EJP
              It won't if the process produces enough output on stderr to block itself.