...
An inefficient solution would be to exhaust the output (or stderr) stream before beginning to wait for the process. A better option is to empty both the stderr and output streams. The code below shows this but is not the best solution since it does not process any arguments passed to the external program (notemaker
) and in turn exits with an OS-specific non-zero exit code.
Code Block | ||
---|---|---|
| ||
import java.io.InputStream; import java.io.InputStreamReader; import java.io.BufferedReader; public class Exec { public static void main(String args[]) { try { Runtime rt = Runtime.getRuntime(); Process proc = rt.exec("notemaker"); InputStream stderr = proc.getErrorStream(); InputStreamReader isr = new InputStreamReader(stderr); BufferedReader br = new BufferedReader(isr); String line; while ( (line = br.readLine()) != null) System.out.println(line); //prints the error lines int exitVal = proc.waitFor(); } catch (Throwable t) { t.printStackTrace(); } } } |
...