1 // Create a process and read from its standard error. 2 import java.io.BufferedReader; 3 import java.io.InputStream; 4 import java.io.InputStreamReader; 5 6 7 public class Process_2 8 { main(String[] args)9 public static void main(String[] args) 10 { 11 try 12 { 13 Runtime r = Runtime.getRuntime(); 14 String s = "Hello World"; 15 String[] a = { "sh", "-c", "echo " + s + " >&2" }; 16 Process p = r.exec(a); 17 InputStream is = p.getErrorStream(); 18 InputStreamReader isr = new InputStreamReader(is); 19 BufferedReader br = new BufferedReader(isr); 20 String result = br.readLine(); 21 if (! s.equals(result)) 22 { 23 System.out.println("bad 1"); 24 return; 25 } 26 result = br.readLine(); 27 if (result != null) 28 { 29 System.out.println("bad 2"); 30 return; 31 } 32 int c = p.waitFor(); 33 System.out.println(c == 0 ? "ok" : "bad 3"); 34 } 35 catch (Exception ex) 36 { 37 System.out.println(ex.toString()); 38 } 39 } 40 } 41