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

Joined
Mar 17, 2010
Messages
1
Reaction score
0
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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,701
Latest member
XavierQ83

Latest Threads

Top