title: Process output from apache-commons exec
body: <p>I am at my wits end here. I'm sure this is something simple and I most likely have huge holes in my understanding of java and streams. I think there are so many classes that I'm a bit overwhelmed with trying to poke through the API to figure out when and how I want to use the multitude of input/output streams.</p>
  
  <p>I just learned about the existence of the apache commons library (self teaching java fail), and am currently trying to convert some of my Runtime.getRuntime().exec to use the commons - exec. Already it's fixed some of the once every 6 months this problem crops up then goes away style problems with exec.</p>
  
  <p>The code executes a perl script, and displays the stdout from the script in the GUI as it is running. </p>
  
  <p>The calling code is inside of a swingworker.</p>
  
  <p>I'm getting lost how to use the pumpStreamHandler... anyway here is the old code:</p>
  
  <pre><code>String pl_cmd = "perl script.pl"
  Process p_pl = Runtime.getRuntime().exec( pl_cmd );
  
  BufferedReader br_pl = new BufferedReader( new InputStreamReader( p_pl.getInputStream() ) );
  
  stdout = br_pl.readLine();
  while ( stdout != null )
  {
      output.displayln( stdout );
      stdout = br_pl.readLine();
  }
  </code></pre>
  
  <p>I guess this is what I get for copy pasting code I don't fully understand a long time ago. The above I assume is executing the process, then grabs the outputstream (via "getInputStream"?), places it into a buffered reader, then will just loop there until the buffer is empty. </p>
  
  <p>What I don't get is why there is no need for a 'waitfor' style command here? Isn't it possible that there will be some time in which the buffer will be empty, exit the loop, and continue on while the process is still going?  When I run it, this doesn't seem to be the case.</p>
  
  <p>In any event, I'm trying to get the same behavior using commons exec, basically again going from google found code:</p>
  
  <pre><code>DefaultExecuteResultHandler rh = new DefaultExecuteResultHandler();
  ExecuteWatchdog wd  = new ExecuteWatchdog( ExecuteWatchdog.INFINITE_TIMEOUT );
  Executor exec = new DefaultExecutor();
  
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  PumpStreamHandler psh = new PumpStreamHandler( out );
  
  exec.setStreamHandler( psh );
  exec.setWatchdog( wd );
  
  exec.execute(cmd, rh );
  rh.waitFor();
  </code></pre>
  
  <p>I'm trying to figure out what pumpstreamhandler is doing. I assume that this will take the output from the exec object, and fill the OutputStream I provide it with the bytes from the perl script's stdout/err?</p>
  
  <p>If so how would you get the above behavior to have it stream the output line by line? In examples people show you call the out.toString() at the end, and I assume this would just give me a dump of all the output from the script once it is done running? How would you do it such that it would show the output as it is running line by line?</p>
  
  <p>------------Future Edit ---------------------</p>
  
  <p>Found this via google and works nice as well:</p>
  
  <pre><code>public static void main(String a[]) throws Exception
  {
      ByteArrayOutputStream stdout = new ByteArrayOutputStream();
      PumpStreamHandler psh = new PumpStreamHandler(stdout);
      CommandLine cl = CommandLine.parse("ls -al");
      DefaultExecutor exec = new DefaultExecutor();
      exec.setStreamHandler(psh);
      exec.execute(cl);
      System.out.println(stdout.toString());
  }
  </code></pre>