Process p = Runtime.getRuntime().exec(String cmd) not reading input stream on Linux

Discussion in 'Java' started by nmlaney@gmail.com, Sep 20, 2006.

  1. Guest

    I'm attempting to use this code on Linux, and only an empty String is
    being logged. I tried this with String line = input.readline() and I'm
    getting other debug stmts. I get the response on Windows, but not
    Linux. What am I missing?

    Process p = Runtime.getRuntime().exec(cmd);
    BufferedReader input = new BufferedReader(new
    InputStreamReader(p.getInputStream()));
    int ch;
    StringBuffer sb = new StringBuffer("");
    while ((ch = input.read()) != -1) {
    sb.append(ch);
    }
    logger.debug(sb.toString());
    input.close();
     
    , Sep 20, 2006
    #1
    1. Advertising

  2. <> wrote in message
    news:...
    > I'm attempting to use this code on Linux, and only an empty String is
    > being logged. I tried this with String line = input.readline() and I'm
    > getting other debug stmts. I get the response on Windows, but not
    > Linux. What am I missing?
    >
    > Process p = Runtime.getRuntime().exec(cmd);
    > BufferedReader input = new BufferedReader(new
    > InputStreamReader(p.getInputStream()));
    > int ch;
    > StringBuffer sb = new StringBuffer("");
    > while ((ch = input.read()) != -1) {
    > sb.append(ch);
    > }
    > logger.debug(sb.toString());
    > input.close();


    There could be several things wrong here, although if you're sure it writes
    a blank entry to the log (ie timestamp and no data) it seems likely that the
    input stream actually contains nothing and does complete. It would guess
    that it's giving you an error message on stderr which you are not showing.
    It's probably saying it can't find the program (exec has different $PATH),
    or that you're expecting the cmd to perform a shell operation (e.g. piping
    output, etc). Find out what the error message says and show us what you're
    putting into cmd.

    In general you must read both stdout and stderr simultaneously (google for
    StreamGobbler) because if either fills the buffer it will block and create
    deadlock as your program will be waiting for more input. This is in the
    Java Gotcha's.

    Matt Humphrey http://www.iviz.com/
     
    Matt Humphrey, Sep 20, 2006
    #2
    1. Advertising

  3. Guest

    Re: Process p = Runtime.getRuntime().exec(String cmd) not reading input stream on Linux

    wrote:
    > I'm attempting to use this code on Linux, and only an empty String is
    > being logged. I tried this with String line = input.readline() and I'm
    > getting other debug stmts. I get the response on Windows, but not
    > Linux. What am I missing?


    It has been awhile but I remember needing to read both stdin and
    stderr. I've done a quite google and I'm going to direct you to this
    link (http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html)
    for starters. The reading in the good example is done in separate
    threads for stdin and stderr. Good luck.

    Cheers,

    Dan Andrews
    - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Ansir Development Limited http://www.ansir.ca
    - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     
    , Sep 20, 2006
    #3
  4. On 20 Sep 2006 13:12:28 -0700, wrote:
    > I'm attempting to use this code on Linux, and only an empty String
    > is being logged. I tried this with String line = input.readline()
    > and I'm getting other debug stmts. I get the response on Windows,
    > but not Linux. What am I missing?


    Others have already addressed the need to read from both streams
    (output and error). I'll add that ProcessBuilder lets you combine them
    into one, which could make things easier for the Java application.

    Also, I'm curious what your command line looks like. Do any of the
    command arguments contain spaces or otherwise need quoting? Are you
    passing wildcards to the command, or doing anything else that requires
    a shell? Are you starting a shell? Post a concrete example.

    /gordon

    --
    [ don't email me support questions or followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Sep 21, 2006
    #4
  5. vivek_kothari

    Joined:
    Jan 12, 2010
    Messages:
    2
    How To Give Input To Process Created By Runtime.getRuTime().exec

    Hi
    I am Trying to run a c program through JAVA. The C Program is

    #include
    int main()
    {
    printf("Enter a number:\n");
    }

    and the JAVA code for invoking is

    import java.io.*;
    public class TimePass implements Runnable{
    Process pro;
    BufferedReader inputReaderForProcess;
    BufferedReader errorReaderForProcess;
    BufferedReader inputReaderForUser;
    Thread tProcessInput,tProcessError,tProcessUserInput;
    BufferedOutputStream wr;
    public TimePass(){
    tProcessInput=new Thread(this);
    tProcessError=new Thread(this);
    tProcessUserInput=new Thread(this);
    try {
    //pro=Runtime.getRuntime().exec("sh /home/globus/workspace/TimePass/bin/asd.sh");
    pro=Runtime.getRuntime().exec("/home/globus/my");
    /* try {
    pro.waitFor();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }*/
    } catch (IOException e) {
    e.printStackTrace();
    }
    tProcessInput.start();
    tProcessError.start();
    tProcessUserInput.start();
    }
    public static void main(String[] args) {
    new TimePass();
    }
    @Override
    public void run() {
    String line;
    if(Thread.currentThread()==tProcessError){
    errorReaderForProcess=new BufferedReader(new InputStreamReader(pro.getErrorStream()));
    try {
    while((line=errorReaderForProcess.readLine())!=nul l){
    line="ERROR > "+ line;
    System.out.println(line);
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    if(Thread.currentThread()==tProcessInput){
    inputReaderForProcess=new BufferedReader(new InputStreamReader(pro.getInputStream()));
    try {
    while((line=inputReaderForProcess.readLine())!=nul l){
    line="OUTPUT > "+ line;
    System.out.println(line);
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    if(Thread.currentThread()==tProcessUserInput){
    wr=new BufferedOutputStream(pro.getOutputStream());
    try {
    wr.write(0);
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    }
     
    vivek_kothari, Jan 12, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Zsolt Koppany
    Replies:
    2
    Views:
    7,189
    SaintMagoo
    Mar 5, 2011
  2. MARTIN Herve \(EXT\)
    Replies:
    1
    Views:
    1,475
    Anthony Borla
    Nov 27, 2003
  3. Hal Vaughan
    Replies:
    11
    Views:
    1,154
    Gordon Beaton
    May 22, 2006
  4. =?ISO-8859-1?Q?Sch=FCle_Daniel?=

    [exec cmd for cmd in cmds]

    =?ISO-8859-1?Q?Sch=FCle_Daniel?=, Mar 8, 2006, in forum: Python
    Replies:
    3
    Views:
    397
    Scott David Daniels
    Mar 8, 2006
  5. SamuelXiao
    Replies:
    16
    Views:
    2,054
    Arne Vajhøj
    Sep 27, 2011
Loading...

Share This Page