public static ProcessExecutionResult getResultFromProcess ( String [ ] args ) { try { Process process = Runtime . getRuntime ( ) . exec ( args ) ; StringBuilder outputSb = new StringBuilder ( ) ; try ( BufferedReader processOutputReader = new BufferedReader ( new InputStreamReader ( process . getInputStream ( ) ) ) ) { String line ; while ( ( line = processOutputReader . readLine ( ) ) != null ) { outputSb . append ( line ) ; outputSb . append ( LINE_SEPARATOR ) ; } } StringBuilder errorSb = new StringBuilder ( ) ; try ( BufferedReader processErrorReader = new BufferedReader ( new InputStreamReader ( process . getErrorStream ( ) ) ) ) { String line ; while ( ( line = processErrorReader . readLine ( ) ) != null ) { errorSb . append ( line ) ; errorSb . append ( LINE_SEPARATOR ) ; } } process . waitFor ( ) ; return new ProcessExecutionResult ( process . exitValue ( ) , outputSb . toString ( ) . trim ( ) , errorSb . toString ( ) . trim ( ) ) ; } catch ( IOException e ) { System . err . println ( "Failed to execute command." ) ; return new ProcessExecutionResult ( 1 , "" , e . getMessage ( ) ) ; } catch ( InterruptedException e ) { Thread . currentThread ( ) . interrupt ( ) ; System . err . println ( "Interrupted." ) ; return new ProcessExecutionResult ( 1 , "" , e . getMessage ( ) ) ; } }
Executes a command in another process and check for its execution result .