Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2011
    Posts
    10
    Rep Power
    0

    Default Using ProcessBuilder

    Hi, I am trying to use ProcessBuilder to run/interact an external application from java, but I have no experience on using ProcessBuilder

    I have created a new process using
    ProcessBuilder pb = new ProcessBuilder("gprolog");

    Set the environment variables using
    pb.environment().put("LINEDIT", "gui=no");

    tried to create outputStream to give input to the program,
    tried to create input stream to get output from the program,
    tried to create input stream to get errors from the program

    But failed at these three.
    Can somebody please explain me how to interact with the process?

    Thank you

  2. #2
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    When you call the ProcessBuilder's start() method, it returns a Process object, and that's the one you want to interact with. Here's where it gets weird: get the Process's stdout and stderr streams as InputStreams, and its stdin stream as an OutputStream. It seems backwards, but it makes sense if you look at it from your program's perspective, not the Process's.

  3. #3
    Join Date
    Feb 2011
    Posts
    10
    Rep Power
    0

    Default

    I understand why input to the program is an output stream and output from the program is an input stream I just cant write that as code, I create the streams but I cant find how to use the streams to send-get information. Can you help me with a little code please ?

  4. #4
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    Quote Originally Posted by efebatistaarda View Post
    I understand why input to the program is an output stream and output from the program is an input stream I just cant write that as code, I create the streams but I cant find how to use the streams to send-get information. Can you help me with a little code please ?
    Ok. I don't know anything about gprolog, but it should go something like this:
    Java Code:
    ProcessBuilder pb = new ProcessBuilder("gprolog");
    Process proc = pb.start();
    PrintWriter out = new PrintWriter(new OutputStreamWriter(proc.getOutputStream()));
    BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
    
    // feed in the program
    out.println(<program string>);
    out.flush();
    
    String resultLine = in.readLine();
    
    while (resultLine != null) {
      System.out.println(resultLine);
      resultLine = in.readLine();
    }
    
    proc.destroy();
    The Process Javadocs recommend buffering the input and output, FWIW.

  5. #5
    Join Date
    Feb 2011
    Posts
    10
    Rep Power
    0

Similar Threads

  1. Using ProcessBuilder
    By akhil in forum New To Java
    Replies: 0
    Last Post: 07-21-2009, 10:12 PM
  2. Run ant scripts using ProcessBuilder
    By prsindagi in forum Advanced Java
    Replies: 6
    Last Post: 02-11-2009, 10:26 AM
  3. bug (?) in ProcessBuilder.redirectErrorStream(true);
    By gadasie in forum Advanced Java
    Replies: 3
    Last Post: 03-13-2008, 09:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •