Launching system processes from Java applications

Are you tired of issues with Runtime.getRuntime().exec()? I was!

So I decided to create a small library to handle all the issues. Once and for all.

Main features

  • It's easy to redirect stdin, stderr and stdout to appropriate Java Input and Output streams, so you can now embed operating-system processes inside your applications.
  • It's very easy to cancel a running process, by using the Future<Integer> interface.
  • Automatically handles pipipng process streams into Java streams (and viceversa).
  • Runs well in Linux and Windows. Probably runs well in Solaris and MacOSX too (haven't tried yet though)
  • You can specify a working directory for the running process.
  • Very small library (just one final class and two interfaces).

Sources and binaries are available in my attic, of course.

What for?

This small library will allow me to embed Scheme interpreters such as guile or scm or mzscheme inside the Scheme IDE I'm building on top of the NetBeans Platform.

How to use?

You can see the Javadoc. Basic usage is as follows:

// Obtain a process executor
ProcessExecutor executor =
   ProcessExecutorFactory.getDefault();

// Launch the process
Future<Integer> process = executor.execute(
   ProcessInputOutput.DEFAULT, null, "/bin/ls -ali");

// If you want to kill the process write:
process.cancel( true );


// To get the exit status of the process use:
System.out.println( process.get() );

Now, that's easy, isn't it?

What next?

As usual I'll do some more extra testing and, who knows, maybe this could be integrated in NetBeans in the future!

All bugs and enhancements are, of course, welcome. Please send me an email or add a comment to this entry.

Happy processing,

Antonio

blog comments powered by Disqus