title: Executing an external program using process builder or apache commons exec
body: <p>I need to execute an external application which returns large data (takes more than 2 hours to complete ) nand which continuously outputs data.</p>
  
  <p>What I need to do is execute this program asynchronously and capture the output in a file.
  I tried using java process builder, however it seems to hang and return output only when the program is exited or forcefully terminated.</p>
  
  <p>I tried to use process builder and spwaned a new thread to capture the output, but still it did not help.</p>
  
  <p>Then I read about apache commons exec and tried the same . however this also seems to be taking a long time and returns different error codes ( for the same input)</p>
  
  <pre><code>CommandLine cmdLine = new CommandLine("/opt/testsimulator");
  
      DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
      ByteArrayOutputStream stdout = new ByteArrayOutputStream();
      PumpStreamHandler psh = new PumpStreamHandler(stdout);
      ExecuteWatchdog watchdog = new ExecuteWatchdog(60*1000);
      Executor executor = new DefaultExecutor();
      executor.setStreamHandler(psh);
      executor.setWatchdog(watchdog);
      try {
          executor.execute(cmdLine);
      } catch (ExecuteException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  </code></pre>
  
  <p>Any help or working examples whould be very helpful</p>