Process process = Runtime.getRuntime().exec(commandAndArgs); OutputReader stdOutReader = new OutputReader(process.getInputStream(), maxBuffer); OutputReader stdErrReader = new OutputReader(process.getErrorStream(), maxBuffer); t2.join(); String out = stdOutReader.getOutput(); String err = stdErrReader.getOutput();
/** * Executes an external command and provides results of execution. Will accummulate limited output from the external process. * * @param command array containing the command to call and its arguments. * * @return instance of {@link Response} with result of execution. */ public static Response execute(String ... command) { try { Process process = Runtime.getRuntime().exec(command); OutputReader stdOutReader = new OutputReader(process.getInputStream()); OutputReader stdErrReader = new OutputReader(process.getErrorStream()); Thread t1 = new Thread(stdOutReader); t1.start(); Thread t2 = new Thread(stdErrReader); t2.start(); int code = process.waitFor(); t1.join(); t2.join(); String out = stdOutReader.getOutput(); String err = stdErrReader.getOutput(); return new Response(out, err, code); } catch (InterruptedException ie) { Thread.currentThread().interrupt(); throw new RuntimeException("Interrupted"); }catch(IOException e){ throw new RuntimeException(e); } }