Okay - This one is a bit of a problem for a lot of folks - Since I have yet to see an answer that works, I thought I would express the problem so someone who has figured it out can tell the rest of us.
The problem is that two out of three of the below work just fine -same code.
The instance for reader3 demonstrates the problem. Reader3 cannot read the result of the successful launch of an external file. Attempting to do any type of read (realine, etc.) on either the stdin or stderr InputStream blocks forever:
package Problems;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class RunningProblem {
public static class RunningReader implements Runnable {
private Process proc;
private String sName;
private RunningReader(Process proc1, String sName) {
this.proc = proc1;
this.sName = sName;
}
public void run() {
try {
// InputStreamReader in = new InputStreamReader(proc.getInputStream());
// BufferedReader reader = new BufferedReader(in);
InputStreamReader err = new InputStreamReader(proc.getErrorStream());
BufferedReader reader = new BufferedReader(err);
String line = reader.readLine();
while (line != null) {
System.out.println(sName + ": " + line);
line = reader.readLine();
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
ExecutorService pool = Executors.newFixedThreadPool(3);
try {
Runtime rt = Runtime.getRuntime();
Process proc1 = rt.exec("ps ax");
RunningReader reader1 = new RunningReader(proc1, "reader1");
Process proc2 = rt.exec("ls -l /");
RunningReader reader2 = new RunningReader(proc2, "reader2");
Process proc3 = rt.exec("/bin/tar");
RunningReader reader3 = new RunningReader(proc3, "reader3");
pool.execute(reader3);
pool.execute(reader2);
pool.execute(reader1);
} catch (Exception ex) {
System.err.println(ex.getMessage());
} finally {
pool.shutdown();
}
System.out.println("Launcher.main() Exited.");
}
}