Child pages
  • Calling Commandline Applications
Skip to end of metadata
Go to start of metadata

The Project Wonder way

Project Wonder, in the ERExtensions framework, have a utility class to help you run command-line applications, ERXRuntimeUtilities. This utility class is used inside Wonder, for example ERAttachment use it to call ImageMagick when you want to resize images.

Check the Javadoc for the execute method to see examples.

The Java way

Here, the "cat" and "outputCat" are debugging output categories:

Process process=null;
try {
  process = Runtime.getRuntime().exec(commandLine);
  OutputStream output = process.getOutputStream();
  DataInputStream dis=new DataInputStream(process.getInputStream());
  String output;
   do {
          output = dis.readLine();
          if (output != null)
      } while (output != null);
} catch (IOException e) {
  cat.error("IOException: " + e.toString());
} catch (InterruptedException e) {
  cat.error("Interrupted process: " + e.toString());
} finally {
  if (process != null)
  outputCat.debug("Process finished.");

Note: A number of people have reported problems with process.waitFor() on Windows. The WebObjects Development List at Omnigroup has a number of people's workaround code for this problem.

Note 2: The procedure given here, of course, is to call anything executable from the command line from your Java program, not just Perl scripts.

Mike Schrag

process.waitFor() is not just a problem on Windows. This code will cause problems. Process maintains buffers for stdout and stderr. If you do not consume those streams, you will run into deadlocks. The proper way to use Runtime.exec is to setup a thread for stderr and a thread for stdout and consume them yourself into your own buffer that does not have the same restrictions that the stock VM has.

There's a good example of this technique on JavaWorld.


  1. Since this is a generic Java thing, should we keep that in the wiki?

  2. While not strictly WO related, it's something everybody needs to do at some point and it's not trivial to get it right. See Mike's comment and the link therein.

    We should add info on Wonder's ERXRuntimeUtilities here that avoids the problems mentioned above.