Process.getInputStream() issue when moving from Windows to Linux

Discussion in 'Java' started by Gabriele82, Mar 17, 2010.

  1. Gabriele82

    Gabriele82

    Joined:
    Mar 17, 2010
    Messages:
    1
    Hi all. I have a Java system that runs commands using Exec. Standard output is quite long so i used a dual threaded approach to consume standard output and standard error (and avoid Exec to starve awaiting the buffer to be freed) and save it to files for later processing. Code is significantly similar to this example, found on the net, and further adapted to my purposes (the example discard the output, i save it to file).

    import java.lang.*;
    import java.io.*;
    public class StreamGobbler implements Runnable {
    String name;
    InputStream is;
    Thread thread;

    public StreamGobbler (String name, InputStream is) {
    this.name = name;
    this.is = is;
    }

    public void start () {
    thread = new Thread (this);
    thread.start ();
    }

    public void run () {
    try {
    InputStreamReader isr = new InputStreamReader (is);
    BufferedReader br = new BufferedReader (isr);

    while (true) {
    String s = br.readLine ();
    if (s == null) break;
    System.out.println ("[" + name + "] " + s);
    }

    is.close ();

    } catch (Exception ex) {
    System.out.println ("Problem reading stream " + name + "... :" + ex);
    ex.printStackTrace ();
    }
    }
    }



    public class SafeProgA {
    public static void main(String[] args) throws Exception {
    Runtime rt = Runtime.getRuntime();
    Process p = rt.exec("Whatever your exec does");
    StreamGobbler s1 = new StreamGobbler ("stdin", p.getInputStream ());
    StreamGobbler s2 = new StreamGobbler ("stderr", p.getErrorStream ());
    s1.start ();
    s2.start ();
    p.waitFor();
    System.out.println("Process Returned");
    }
    }

    It worked flawlessly on Windows.
    When moving to Linux i'm experimenting a few problems.
    One, solved by now, is the fact that on Linux (Java 1.6 freshly installed) br.readLine () throws an exception if the underlying stream is closed, instead of returning null as it is supposed to do. I cacthed the exception and worked around it.

    The other problem, which i'm facing right now, is the fact that the saved file appear to not fully reflect the output stream. I mean, it misses some part in the end. It seems that the gobblers doesn't have time to read the streams, and/or this stream get closed before the gobblers read it in full. Maybe the stream is not flushed for a while and then flushed and closed immediatly thereafter or directly closed and not flushed at all, making hard for the gobblers to grab the data.

    Given that this issue was completely absent on Windows and the output files always appeared to be complete, could you futher suggest what problem could cause that and how to guarantee that my gobblers get the chance to read and save the standard output before this is closed?
     
    Gabriele82, Mar 17, 2010
    #1
    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. Joana
    Replies:
    5
    Views:
    5,507
    John C. Bollinger
    Sep 16, 2003
  2. b83503104
    Replies:
    1
    Views:
    28,341
    John C. Bollinger
    Dec 1, 2003
  3. igoR Buttler
    Replies:
    1
    Views:
    12,241
  4. Syed Ali
    Replies:
    1
    Views:
    848
    kpl_pl
    Jul 20, 2007
  5. vicky7909@rediffmail.com

    Socket error Windows getInputStream

    vicky7909@rediffmail.com, Mar 18, 2008, in forum: Java
    Replies:
    9
    Views:
    2,907
Loading...

Share This Page