com.perdues
Class SyncExec

java.lang.Object
  |
  +--com.perdues.SyncExec

public class SyncExec
extends java.lang.Object

Utility class for running a subprocess synchronously from Java and retrieving the contents of System.out and/or system.err.


Inner Class Summary
static class SyncExec.CharPump
           
static class SyncExec.Exception
           
 
Method Summary
static java.lang.String exec(java.lang.String[] cmd)
          Runs a process using Runtime.exec().
static int exec(java.lang.String[] cmd, java.io.Writer output, java.io.Writer errors)
          Runs a process using Runtime.exec().
static void main(java.lang.String[] args)
          Main routine, for testing.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

exec

public static java.lang.String exec(java.lang.String[] cmd)
                             throws java.io.IOException,
                                    java.lang.InterruptedException
Runs a process using Runtime.exec(). Collects all output on System.out into a String and returns it, waiting for all output to System.out and System.err, and for completion of the child process. If the exit status is nonzero or there is any output to System.err, this throws a com.perdues.SyncExec.Exception containing the exit status and error output.

exec

public static int exec(java.lang.String[] cmd,
                       java.io.Writer output,
                       java.io.Writer errors)
                throws java.io.IOException,
                       java.lang.InterruptedException
Runs a process using Runtime.exec(). Collects all output on System.out and System.err, passing it to the two Writer parameters. Waits for all output and process completion, writing to the two Writers via the system default character encoding, then returns the process exit status.

Note the if one of the Writers blocks, this may block the child process.


main

public static void main(java.lang.String[] args)
Main routine, for testing. The name of the executable to run and its arguments are the command line arguments.