I need to execute an external application which returns large data (takes more than 2 hours to complete ) nand which continuously outputs data.
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.
I tried to use process builder and spwaned a new thread to capture the output, but still it did not help.
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)
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();
}
Any help or working examples whould be very helpful
Huh. Using
ProcessBuilder
should work for your configuration. For example, the following pattern works for me:The program that I'm running is just a script with a whole bunch of
sleep 1
andecho
lines:The thread reading from the process spits out a
Hello
every second or so.