private static void executeCommand( String[] cmd, File directory ) throws IOException, InterruptedException { String command = ""; for ( int i = 0; i < cmd.length; i++ ) { command += " " + cmd[i]; } log.logBasic( "Jar generator", "Executing command : " + command ); Runtime runtime = java.lang.Runtime.getRuntime(); Process proc = runtime.exec( cmd, EnvUtil.getEnvironmentVariablesForRuntimeExec(), directory ); // any error message? StreamLogger errorLogger = new StreamLogger( log, proc.getErrorStream(), "Jar generator (stderr)" ); // any output? StreamLogger outputLogger = new StreamLogger( log, proc.getInputStream(), "Jar generator (stdout)" ); // kick them off new Thread( errorLogger ).start(); new Thread( outputLogger ).start(); proc.waitFor(); log.logDetailed( "Jar generator", "command [" + cmd[0] + "] has finished" ); // What's the exit status? if ( proc.exitValue() != 0 ) { log.logDetailed( "Jar generator", "Exit status of jar command was " + proc.exitValue() ); } // close the streams // otherwise you get "Too many open files, java.io.IOException" after a lot of iterations try { proc.getErrorStream().close(); proc.getInputStream().close(); } catch ( IOException e ) { log.logDetailed( "Jar generator", "Warning: Error closing streams: " + e.getMessage() ); } }
data.cmdProc = runtime.exec( cmdstr, EnvUtil.getEnvironmentVariablesForRuntimeExec() ); data.writer = data.cmdProc.getOutputStream();