External process execution

Objectives:

  1. Provide an easy-to-use API for launching operating system commands.
  2. Be able to redirect stdin, stderr and stdout to arbitrary Java input and output streams, so that you can embed the running process more easily in your Java programs.
  3. Be able to specify a working directory for the new process.
  4. Correctly clean-up resources when a problem happens.
  5. Execute the process asynchronously, while being able to cleanly cancel it.

Basic use:

Obtain a ProcessExecutor from the ProcessExecutorFactory:

  ProcessExecutor cli = ProcessExecutorFactory.getDefault();
    

Then launch an operating system command:

  Future<Integer> result =
    cli.execute( ProcessStreams.DEFAULT, null, "/bin/ls -ali" );
    

The process is started asynchronously.

  System.out.println( result.get() );
    

And you can cancel it like this:

  result.cancel( true );
    

Advanced use

You can redirect stdin, stderr and stdout of running processes to any Java Input and Output sreams. To do so, just implement the ProcessStreams interface and pass it to the "execute" method of your ProcessExecutor.