public class Exec
extends java.lang.Thread
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.| Modifier and Type | Class and Description |
|---|---|
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.
|
static class |
Exec.OutputStreamChecker
Check for a string passed to the OutputStream.
|
static interface |
Exec.OutputStreamCheckerListener
Interface for objects to be notified immediately when OutputStreamChecker
matches when it is looking for.
|
| Constructor and Description |
|---|
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.
|
| Modifier and Type | Method and Description |
|---|---|
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.
|
activeCount, checkAccess, clone, 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, yieldpublic Exec(java.lang.String command,
java.lang.String[] envVars,
java.io.File dir,
java.io.OutputStream outStreamRedir,
java.io.OutputStream errStreamRedir)
/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.
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 processoutStreamRedir - stdout of the process will be redirected to this stream if not nullerrStreamRedir - stderr of the process will be redirected to this stream if not nullpublic Exec(java.lang.String[] exec,
java.lang.String[] envVars,
java.io.File dir,
java.io.OutputStream outStreamRedir,
java.io.OutputStream errStreamRedir)
exec - the executable and arguments of the processenvVars - 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 processoutStreamRedir - stdout of the process will be redirected to this stream if not nullerrStreamRedir - stderr of the process will be redirected to this stream if not nullpublic void run()
run in interface java.lang.Runnablerun in class java.lang.Threadpublic void writeln(java.lang.String line)
line - a line of text to send to the processpublic void addFinishedListener(Exec.FinishedListener a)
a - the listenerpublic void removeFinishedListener(Exec.FinishedListener a)
a - the listenerpublic void destroyProcess()
public int getExitVal()