I think you need to wait until the process is over. I implemented something like this as follows:
public class ProcessReader { private static final int PROCESS_LOOP_SLEEP_MILLIS = 100; private String result; public ProcessReader(Process process) { BufferedReader resultReader = new BufferedReader(new InputStreamReader(process.getInputStream())); StringBuilder resultOutput = new StringBuilder(); try { while (!checkProcessTerminated(process, resultReader, resultOutput)) { } } catch (Exception ex1) { throw new RuntimeException(ex1); } result = resultOutput.toString(); } public String getResult(){ return result; } private boolean checkProcessTerminated(Process process, BufferedReader resultReader, StringBuilder resultOutput) throws Exception { try { int exit = process.exitValue(); return true; } catch (IllegalThreadStateException ex) { Thread.sleep(PROCESS_LOOP_SLEEP_MILLIS); } finally { while (resultReader.ready()) { String out = resultReader.readLine(); resultOutput.append(out).append("\n"); } } return false; }
}
I just deleted some specific code that you don't need, but it should work, try it. Relations
source share