com.sun.electric.tool.user
Class Exec

java.lang.Object
  extended by java.lang.Thread
      extended by com.sun.electric.tool.user.Exec
All Implemented Interfaces:
java.lang.Runnable

public class Exec
extends java.lang.Thread

Runtime.exec() has many pitfalls to it's proper use. This class wraps external executes to make it easier to use.

Usage:

 Exec exec = new Exec("ls", null, User.getWorkingDirectory(), System.out, System.out);
 exec.start(); // run in a new Thread
 
You can also use exec.run() to run in the current thread.


Nested Class Summary
static class Exec.ExecProcessReader
          This class is used to read data from an external process.
static class Exec.FinishedEvent
          The event passed to listeners when the process finishes
static interface Exec.FinishedListener
          Objects that want to be notified of the process finishing should implement this interface, and add themselves as a listener to the process.
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
Exec(java.lang.String[] exec, java.lang.String[] envVars, java.io.File dir, java.io.OutputStream outStreamRedir, java.io.OutputStream errStreamRedir)
          Execute an external process.
Exec(java.lang.String command, java.lang.String[] envVars, java.io.File dir, java.io.OutputStream outStreamRedir, java.io.OutputStream errStreamRedir)
          Execute an external process.
 
Method Summary
 void addFinishedListener(Exec.FinishedListener a)
          Add a Exec.FinishedListener
 void destroyProcess()
          End this process, if it is running.
 int getExitVal()
           
 void removeFinishedListener(Exec.FinishedListener a)
          Remove a Exec.FinishedListener
 void run()
           
 void writeln(java.lang.String line)
          Send a line of text to the process.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Exec

public Exec(java.lang.String command,
            java.lang.String[] envVars,
            java.io.File dir,
            java.io.OutputStream outStreamRedir,
            java.io.OutputStream errStreamRedir)
Execute an external process. Note: command is not a shell command line command, it is a single program and arguments. Therefore, /bin/sh -c /bin/ls > file.txt will NOT work.

Instead, use String[] exec = {"/bin/sh", "-c", "/bin/ls > file.txt"}; and use the other constructor.

Parameters:
command - the command to run.
envVars - environment variables of the form name=value. If null, inherits vars from current process.
dir - the working directory. If null, uses the working dir from the current process
outStreamRedir - stdout of the process will be redirected to this stream if not null
errStreamRedir - stderr of the process will be redirected to this stream if not null

Exec

public Exec(java.lang.String[] exec,
            java.lang.String[] envVars,
            java.io.File dir,
            java.io.OutputStream outStreamRedir,
            java.io.OutputStream errStreamRedir)
Execute an external process. Note: this is not a command-line command, it is a single program and arguments.

Parameters:
exec - the executable and arguments of the process
envVars - environment variables of the form name=value. If null, inherits vars from current process.
dir - the working directory. If null, uses the working dir from the current process
outStreamRedir - stdout of the process will be redirected to this stream if not null
errStreamRedir - stderr of the process will be redirected to this stream if not null
Method Detail

run

public void run()
Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread

writeln

public void writeln(java.lang.String line)
Send a line of text to the process. This is not useful if the process is not expecting any input.

Parameters:
line - a line of text to send to the process

addFinishedListener

public void addFinishedListener(Exec.FinishedListener a)
Add a Exec.FinishedListener

Parameters:
a - the listener

removeFinishedListener

public void removeFinishedListener(Exec.FinishedListener a)
Remove a Exec.FinishedListener

Parameters:
a - the listener

destroyProcess

public void destroyProcess()
End this process, if it is running. Otherwise, does nothing


getExitVal

public int getExitVal()