public class StreamExec extends java.lang.Object implements ExecuteCommands
As an alternative to this class, see
JTextAreaExec
, which uses Swing; and
StringBufferExec
, which writes to a StringBuffer.
Sample usage:
List execCommands = new LinkedList(); execCommands.add("date"); execCommands.add("sleep 3"); execCommands.add("date"); execCommands.add("notACommand"); final StreamExec exec = new StreamExec(); exec.setCommands(execCommands); exec.start();
Loosely based on Example1.java from http://java.sun.com/products/jfc/tsc/articles/threads/threads2.html
See also http://developer.java.sun.com/developer/qow/archive/135/index.jsp (1/11: Broken) and http://jw.itworld.com/javaworld/jw-12-2000/jw-1229-traps.html.
JTextAreaExec
,
StringBufferExec
Red (cxh) |
Red (cxh) |
Constructor and Description |
---|
StreamExec()
Create a StreamExec.
|
Modifier and Type | Method and Description |
---|---|
protected void |
_setProgressBarMaximum(int size)
Set the maximum of the progress bar.
|
void |
appendToPath(java.lang.String directoryName)
Append to the path of the subprocess.
|
void |
cancel()
Cancel any running commands.
|
void |
clear()
Clear the text area, status bar and progress bar.
|
java.lang.String |
getenv(java.lang.String key)
Get the value of the environment of the subprocess.
|
int |
getLastSubprocessReturnCode()
Return the return code of the last subprocess that was executed.
|
java.lang.String |
getPatternLog()
Return the value of the pattern log.
|
void |
setCommands(java.util.List commands)
Set the list of commands.
|
void |
setPattern(java.lang.String pattern)
Set the pattern that is used to search data sent to stdout.
|
void |
setWaitForLastSubprocess(boolean waitForLastSubprocess)
Determine whether the last subprocess is waited for or not.
|
void |
setWorkingDirectory(java.io.File workingDirectory)
Set the working directory of the subprocess.
|
void |
start()
Start running the commands.
|
void |
stderr(java.lang.String text)
Append the text message to stderr.
|
void |
stdout(java.lang.String text)
Append the text message to the output.
|
static java.lang.String[] |
updateEnvironment(java.lang.String key,
java.lang.String value)
Update the environment and return the results.
|
void |
updateStatusBar(java.lang.String text)
Set the text of the status bar.
|
public void appendToPath(java.lang.String directoryName)
appendToPath
in interface ExecuteCommands
directoryName
- The name of the directory to append to the path.public void cancel()
cancel
in interface ExecuteCommands
public void clear()
clear
in interface ExecuteCommands
public java.lang.String getenv(java.lang.String key)
getenv
in interface ExecuteCommands
key
- The key for which to search.public java.lang.String getPatternLog()
Calling this method resets the log of previously received matches.
setPattern(String)
,
stdout(String)
public int getLastSubprocessReturnCode()
getLastSubprocessReturnCode
in interface ExecuteCommands
public void setCommands(java.util.List commands)
setCommands
in interface ExecuteCommands
commands
- A list of Strings, where each element is a command.public void setPattern(java.lang.String pattern)
If the value of the pattern argument is non-null, then
each time stdout(String)
is called, the value of
the argument to stdout is compared with the pattern
regex. If there is a match, then the value is appended
to a StringBuffer that whose value may be obtained with
the getPatternLog()
method.
Calling this method resets the log of previously received matches.
pattern
- The pattern used.getPatternLog()
public void setWaitForLastSubprocess(boolean waitForLastSubprocess)
waitForLastSubprocess
- True if the start()
method should wait for the last subprocess to finish.public void setWorkingDirectory(java.io.File workingDirectory)
setWorkingDirectory
in interface ExecuteCommands
workingDirectory
- The working directory of the
subprocess. If this argument is null, then the subprocess is
executed in the working directory of the current process.public void start()
setWaitForLastSubprocess(boolean)
.start
in interface ExecuteCommands
public void stderr(java.lang.String text)
JTextAreaExec
appends to a
JTextArea. The output automatically gets a trailing newline
appended.stderr
in interface ExecuteCommands
text
- The text to append to standard error.public void stdout(java.lang.String text)
JTextAreaExec
appends to a
JTextArea.
The output automatically gets a trailing newline appended.
If setPattern(String)
has been called with a
non-null argument, then any text that matches the pattern
regex will be appended to a log file. The log file
may be read with getPatternLog()
.
stdout
in interface ExecuteCommands
text
- The text to append to standard out.public static java.lang.String[] updateEnvironment(java.lang.String key, java.lang.String value)
key
- The environment variable to be updated.value
- The value to appendname=value
with the environment variable
named by the key parameter updated to include the value
of the value parameter.public void updateStatusBar(java.lang.String text)
updateStatusBar
in interface ExecuteCommands
text
- The text with which the status bar is updated.protected void _setProgressBarMaximum(int size)
size
- The maximum size of the progress bar.