Executing an external program using process builder or apache commons exec

coder picture coder · May 19, 2012 · Viewed 7.1k times · Source

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

Answer

Gray picture Gray · May 19, 2012

Huh. Using ProcessBuilder should work for your configuration. For example, the following pattern works for me:

ProcessBuilder pb = new ProcessBuilder("/tmp/x");
Process process = pb.start();
final InputStream is = process.getInputStream();
// the background thread watches the output from the process
new Thread(new Runnable() {
    public void run() {
        try {
            BufferedReader reader =
                new BufferedReader(new InputStreamReader(is));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            is.close();
        }
    }
}).start();
// the outer thread waits for the process to finish
process.waitFor();

The program that I'm running is just a script with a whole bunch of sleep 1 and echo lines:

#!/bin/sh
sleep 1
echo "Hello"
sleep 1
echo "Hello"
...

The thread reading from the process spits out a Hello every second or so.