@ FFDCIgnore ( { IOException . class , InterruptedException . class } ) public static List < String > executeProgramWithInput ( String input , String ... commandLine ) throws OperatingSystemException { try { ProcessBuilder builder = new ProcessBuilder ( ) ; builder . command ( commandLine ) ; Process process = builder . start ( ) ; if ( input != null ) { try ( OutputStream os = process . getOutputStream ( ) ) { try ( OutputStreamWriter osw = new OutputStreamWriter ( os ) ) { try ( BufferedWriter bw = new BufferedWriter ( osw ) ) { bw . write ( input ) ; bw . flush ( ) ; } } } } List < String > lines = new ArrayList < String > ( ) ; try ( InputStream is = process . getInputStream ( ) ) { try ( InputStreamReader isr = new InputStreamReader ( is ) ) { try ( BufferedReader br = new BufferedReader ( isr ) ) { String line ; while ( ( line = br . readLine ( ) ) != null ) { lines . add ( line ) ; } } } } int returnCode = process . waitFor ( ) ; if ( returnCode != 0 ) { throw new OperatingSystemException ( Tr . formatMessage ( tc , "os.execute.error" , commandLine , returnCode , lines ) ) ; } return lines ; } catch ( IOException e ) { throw new OperatingSystemException ( e ) ; } catch ( InterruptedException e ) { throw new OperatingSystemException ( e ) ; } }
On recent JDKs this should do a fork that doesn t duplicate the whole virtual address space .