com.perdues
Class ProcessUtils

java.lang.Object
  extended by com.perdues.ProcessUtils

public class ProcessUtils
extends Object

Convenient class to run a native command-line program from Java and receive its output and exit status. The simplest method returns the program's standard output to you as a String and throws an exception in case of nonzero exit status or error output. This class was inspired by the convenience of the Tcl "exec" function.


Nested Class Summary
static class ProcessUtils.Exception
          This Exception class reports error exit status, all standard output and error output from an external program.
 
Method Summary
static String exec(String[] cmd)
          Runs a command-line program and returns all of its standard output as a String.
static int exec(String[] cmd, InputStream input, OutputStream output, OutputStream errors)
          Runs a command-line program, writes all of its standard output and error output to two OutputStreams, returns its final exit status, and makes your InputStream available to the program.
static int exec(String[] cmd, OutputStream output, OutputStream errors)
          Runs a command-line program, writes all of its standard output and error output to two OutputStreams, and returns its final exit status.
static int exec(String[] cmd, Reader input, Writer output, Writer errors)
          Runs a command-line program, writes all of its standard output and error output to two Writers, returns its final exit status, and makes your Reader available to the program.
static int exec(String[] cmd, Writer output, Writer errors)
          Runs a command-line program, passes its standard output and error output to two Writers, and returns its final exit status.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

exec

public static String exec(String[] cmd)
                   throws IOException,
                          InterruptedException
Runs a command-line program and returns all of its standard output as a String. If the process exit status is nonzero or there is any output to System.err, this throws a ProcessUtils.Exception containing the exit status and any output. This method allows the process to write any quantity of character data to its standard output and error output using the system default character encoding.

Returns:
the program's standard output as a String.
Throws:
ProcessUtils.Exception - in case of nonzero exit status or error output.
IOException
InterruptedException

exec

public static int exec(String[] cmd,
                       Writer output,
                       Writer errors)
                throws IOException,
                       InterruptedException
Runs a command-line program, passes its standard output and error output to two Writers, and returns its final exit status. Outputs to the Writer arguments using the system default character encoding. Note that this retrieves the Process output and actually writes it to the Writers you provide. Contrast this with java.lang.Runtime.exec, which obliges you to write additional (threaded) code to read from the streams it makes available to you.

Returns:
the program's exit status.
Throws:
IOException
InterruptedException

exec

public static int exec(String[] cmd,
                       Reader input,
                       Writer output,
                       Writer errors)
                throws IOException,
                       InterruptedException
Runs a command-line program, writes all of its standard output and error output to two Writers, returns its final exit status, and makes your Reader available to the program. Waits for all output and process completion, writes it to the Writer arguments using the system default character encoding, then returns the process exit status. Contrast this with java.lang.Runtime.exec, which obliges you to write additional (threaded) code to read from and write to the streams it makes available to you. The Reader argument may be null, and in this case this method supplies no input to the program.

This will complete even if the program only reads part of its input, and before returning it interrupts the thread that feeds input to the program. Reads on some Readers do not unblock on interrupts, so the input thread may live until the last read completes.

Returns:
the program's exit status.
Throws:
IOException
InterruptedException

exec

public static int exec(String[] cmd,
                       OutputStream output,
                       OutputStream errors)
                throws IOException,
                       InterruptedException
Runs a command-line program, writes all of its standard output and error output to two OutputStreams, and returns its final exit status. Waits for all output and process completion, writes it to the OutputStreams, then returns the process exit status. Contrast this with java.lang.Runtime.exec, which obliges you to write additional (threaded) code to read from and write to the streams it makes available to you.

Throws:
IOException
InterruptedException

exec

public static int exec(String[] cmd,
                       InputStream input,
                       OutputStream output,
                       OutputStream errors)
                throws IOException,
                       InterruptedException
Runs a command-line program, writes all of its standard output and error output to two OutputStreams, returns its final exit status, and makes your InputStream available to the program. Waits for all output and process completion, writes it to the OutputStreams, then returns the process exit status. Contrast this with java.lang.Runtime.exec, which obliges you to write additional (threaded) code to read from and write to the streams it makes available to you.

This will complete even if the program only reads part of its input, and before returning it interrupts the thread that feeds input to the program. Reads on some InputStreams do not unblock on interrupts, so the input thread may live until the last read completes.

Returns:
the program's exit status.
Throws:
IOException
InterruptedException