Decision
You should try to read and execute in different threads.
The best alternative is to use ProcessBuilder , which takes care of the dirty work for you.
The code inside the try block might look something like this:
ProcessBuilder pb = new ProcessBuilder(commandArr); pb.redirectErrorStream(true); Process proc = pb.start(); System.out.println("Process started !"); String line; BufferedReader in = new BufferedReader(new InputStreamReader( proc.getInputStream())); while ((line = in.readLine()) != null) { System.out.println(line); } proc.destroy(); System.out.println("Process ended !");
See also this short demo .
Cause of the problem
According to Java Docs , waitFor() :
makes the current thread wait, if necessary, until the process represented by this process object is completed.
So, you are trying to get the output stream of the process after it is finished, therefore null .
(Sorry for the big revision of the answer.)
source share