public static Response execute ( int maxBuffer , String ... command ) { if ( command . length == 0 ) { throw new IllegalArgumentException ( "Command must be provided." ) ; } String [ ] commandAndArgs = command . length == 1 && command [ 0 ] . contains ( " " ) ? Util . split ( command [ 0 ] , " " ) : command ; try { Process process = Runtime . getRuntime ( ) . exec ( commandAndArgs ) ; OutputReader stdOutReader = new OutputReader ( process . getInputStream ( ) , maxBuffer ) ; OutputReader stdErrReader = new OutputReader ( process . getErrorStream ( ) , maxBuffer ) ; 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 ) ; } }
Executes an external command and provides results of execution . Will accumulate limited output from the external process .