How do i get the output of a command?

so i have the following

ArrayList<String> outputBuffer = new ArrayList<String>();
Process p=null;
InputStream error=null;

try {

  // create a new process
  System.out.println("Creating Process... [Executing 1 of 4 possible cases]");
  //p = Runtime.getRuntime().exec("notepad.exe");                                    // An example of good case GOOD 
  //p = Runtime.getRuntime().exec("nnotepad.exe");                                   // Example on ERROR: Typo in name
  //p = Runtime.getRuntime().exec("cmd /c dir /D >> C:\\mySandBox\\procdemo.txt");   //Custom cmd
  p = exec("ping", "-c 4 1.1.1.1");

  final BufferedReader
    out = new BufferedReader(new InputStreamReader(p.getInputStream())), 
    err = new BufferedReader(new InputStreamReader(p.getErrorStream()));

  outputBuffer.clear();
  String read;
  while ((read = out.readLine()) != null) outputBuffer.add(read);

  System.out.println("Size out buffer: " + outputBuffer.size());
  for (String ss : outputBuffer) {
    System.out.println("WATCH2 : " + ss);
  }

  // get the error stream of the process and print it
  error = p.getErrorStream();

  if (error.available() > 0) {
    System.out.println("ERROR DETECTED: ");
    for (int i = 0; i < error.available(); i++) {
      System.out.print( ((char) error.read()));
    }
    System.out.println();
  }

  // wait for 10 seconds and then destroy the process
  Thread.sleep(4000);
  p.destroy();
} 
catch (Exception ex) {
  ex.printStackTrace();
}

but it gives me the following error.

Creating Process… [Executing 1 of 4 possible cases]
Size out buffer: 0
ERROR DETECTED:
ping: invalid count of packet

Any ideas why (on a mac platform) ?

1 Like