...
Code Block | ||
---|---|---|
| ||
class StreamGobbler extendsimplements ThreadRunnable { private final InputStream is; private final PrintStream os; StreamGobbler(InputStream is, PrintStream os) { this.is = is; this.os = os; } public void run() { try { int c; while ((c = is.read()) != -1) os.print((char) c); } catch (IOException x) { // Handle error } } } public class Exec { public static void main(String[] args) throws IOException, InterruptedException { Runtime rt = Runtime.getRuntime(); Process proc = rt.exec("notemaker"); // Any error message? StreamGobblerThread errorGobbler = = new Thread(new StreamGobbler(proc.getErrorStream(), System.err)); // Any output? StreamGobblerThread outputGobbler = = new Thread(new StreamGobbler(proc.getInputStream(), System.out)); errorGobbler.start(); outputGobbler.start(); // Any error? int exitVal = proc.waitFor(); errorGobbler.join(); // Handle condition where the outputGobbler.join(); // process ends before the threads finish } } |
...