Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Process.getInputStream() issue when moving from Windows to Linux

Reply
Thread Tools

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

 
 
Gabriele82 Gabriele82 is offline
Junior Member
Join Date: Mar 2010
Posts: 1
 
      03-17-2010
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?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Moving Guide Moving Companies Movers Storage linkswanted ASP .Net 0 01-23-2008 01:08 AM
Free Moving Estimate, Local Movers, Long Distance Moving, PackingSupplies, Storage Rental, Home Moving, Apartment Moving, Office Moving,Commercial Moving linkswanted ASP .Net 0 01-06-2008 04:45 AM
Moving to California Moving to L.A linkswanted Digital Photography 3 01-05-2008 04:22 PM
movers los angeles moving storage services movers in L.A companymoving companies moving in calfornia los angeles linkswanted HTML 0 12-21-2007 10:55 PM



Advertisments