org.fife.io
Class ProcessRunner

java.lang.Object
  extended by org.fife.io.ProcessRunner
All Implemented Interfaces:
java.lang.Runnable

public class ProcessRunner
extends java.lang.Object
implements java.lang.Runnable

Runs an external process (a program, batch file, shell script, etc.) as safely as possible.

Version:
1.0
Author:
Robert Futrell

Constructor Summary
ProcessRunner(java.lang.String[] commandLine)
          Constructor.
 
Method Summary
 boolean getAppendEnvironmentVars()
          Returns whether any extra environment variables defined for this process to run with should be appended to the parent process's environment (as opposed to overwriting it).
 java.lang.String getCommandLineString()
          Returns the command line this external process runner will run as a string.
 java.io.File getDirectory()
          Returns the directory to run the process in.
 java.util.Map<java.lang.String,java.lang.String> getEnvironmentVars()
          Returns any extra environment variables defined for this process to run with.
 java.lang.Throwable getLastError()
          Returns the last error thrown when trying to run a process, or null if the last process ran successfully.
 int getReturnCode()
          Returns the return code of the last process ran.
 java.lang.String getStderr()
          Returns the stderr of the process last ran.
 java.lang.String getStdout()
          Returns the stdout of the process last ran.
 void run()
          Runs the current external process.
 void setCommandLine(java.lang.String[] commandLine)
          Sets the command line of the process to run.
 void setDirectory(java.io.File dir)
          Sets the directory to run the process in.
 void setEnvironmentVars(java.util.Map<java.lang.String,java.lang.String> vars, boolean append)
          Sets the environment variables to be set for this process.
 void setOutputListener(ProcessRunnerOutputListener listener)
          Sets the output listener to receive notification when stdout or stderr is written to.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ProcessRunner

public ProcessRunner(java.lang.String[] commandLine)
Constructor.

Parameters:
commandLine - The command line to run, with each item in the array being a single parameter.
Throws:
java.lang.IllegalArgumentException - If commandLine has length 0.
java.lang.NullPointerException - If commandLine is null.
Method Detail

getAppendEnvironmentVars

public boolean getAppendEnvironmentVars()
Returns whether any extra environment variables defined for this process to run with should be appended to the parent process's environment (as opposed to overwriting it).

Returns:
Whether to append the parent process's environment.
See Also:
getEnvironmentVars(), setEnvironmentVars(Map, boolean)

getCommandLineString

public java.lang.String getCommandLineString()
Returns the command line this external process runner will run as a string. Parameters are wrapped in quotes.

Returns:
The command line this object will run.
See Also:
setCommandLine(String[])

getDirectory

public java.io.File getDirectory()
Returns the directory to run the process in.

Returns:
The directory the process will run in. If this is null, then the process will run in the same directory as this Java process.
See Also:
setDirectory(File)

getEnvironmentVars

public java.util.Map<java.lang.String,java.lang.String> getEnvironmentVars()
Returns any extra environment variables defined for this process to run with.

Returns:
The environment variables.
See Also:
getAppendEnvironmentVars(), setEnvironmentVars(Map, boolean)

getLastError

public java.lang.Throwable getLastError()
Returns the last error thrown when trying to run a process, or null if the last process ran successfully.

Returns:
The error that the last-run process ended with, if any.

getReturnCode

public int getReturnCode()
Returns the return code of the last process ran.

Returns:
The return code of the last process ran.

getStderr

public java.lang.String getStderr()
Returns the stderr of the process last ran.

Returns:
The stderr of the last process ran.

getStdout

public java.lang.String getStdout()
Returns the stdout of the process last ran.

Returns:
The stdout of the last process ran.

run

public void run()
Runs the current external process.

Specified by:
run in interface java.lang.Runnable
Throws:
java.io.IOException - If an I/O error occurs while running the process.
See Also:
getStdout(), getStderr()

setDirectory

public void setDirectory(java.io.File dir)
Sets the directory to run the process in.

Parameters:
dir - The directory.
See Also:
getDirectory()

setCommandLine

public void setCommandLine(java.lang.String[] commandLine)
                    throws java.lang.IllegalArgumentException
Sets the command line of the process to run.

Parameters:
commandLine - The command line parameters to run.
Throws:
java.lang.IllegalArgumentException - If commandLine has length 0.
java.lang.NullPointerException - If commandLine is null.
See Also:
getCommandLineString()

setEnvironmentVars

public void setEnvironmentVars(java.util.Map<java.lang.String,java.lang.String> vars,
                               boolean append)
Sets the environment variables to be set for this process.

Parameters:
vars - The environment variables. This may be null if none are to be set.
append - Whether this should be appended to the parent process's environment. If this is false, then the contents of vars will be the only environment variables set.
See Also:
getEnvironmentVars()

setOutputListener

public void setOutputListener(ProcessRunnerOutputListener listener)
Sets the output listener to receive notification when stdout or stderr is written to. This listener will be used for all subsequent processes run with this ProcessRunner.

Parameters:
listener - The new listener. The previous listener, if any, will be removed. If this is null, there will be no output listener.