|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectnet.sf.jnati.proc.ProcessMonitor
public class ProcessMonitor
This class is used to create and monitor the running of an operating system process.
Class for running a native process in a separate thread.
ProcessMonitor proc = new ProcessMonitor("/usr/bin/babel", "-ismiles", "-ocml");
proc.setInput("CN(C)CCC1=CNC2=C1C=C(C=C2)CC3COC(=O)N3");
ByteArrayOutputStream out = new ByteArrayOutputStream();
proc.setOutputStream(out);
proc.setErrorStream(System.err);
proc.execsute();
| Nested Class Summary | |
|---|---|
static class |
ProcessMonitor.ProcessState
|
| Constructor Summary | |
|---|---|
ProcessMonitor(List<String> command)
Constructs a monitored process with the specified operating system program and arguments. |
|
ProcessMonitor(String... command)
Constructs a monitored process with the specified operating system program and arguments. |
|
| Method Summary | |
|---|---|
void |
cancel()
Cancel the process monitored by this object. |
int |
execute()
Runs the process, waits for it to complete, and returns its exit status. |
byte[] |
getCachedError()
Returns an array containing the last bytes the process monitored by this object has written to STDERR. |
byte[] |
getCachedOutput()
Returns an array containing the last bytes the process monitored by this object has written to STDOUT. |
List<String> |
getCommand()
Returns this monitored process's operating system program and arguments. |
Map<String,String> |
getEnvironment()
|
Integer |
getExitValue()
|
Throwable |
getFailCause()
Returns any exception while the process is running, or null if no exception was thrown. |
boolean |
getRedirectErrorStream()
|
ProcessMonitor.ProcessState |
getState()
Returns the current state of this process. |
long |
getTimeout(TimeUnit unit)
|
File |
getWorkingDirectory()
Returns this monitored process's working directory. |
ProcessOutput |
runProcess()
Runs the process, waits for it to complete, and returns its output. |
void |
setCommand(List<String> command)
Sets this monitored process's operating system program and arguments. |
void |
setCommand(String... command)
Sets this monitored process's operating system program and arguments. |
void |
setEnvironmentVariable(String key,
String value)
|
void |
setErrorTarget(OutputStream stderr)
Sets the OutputStream to which bytes written to the process' STDERR will be directed. |
void |
setInput(byte[] input)
Sets input for process. |
void |
setInput(InputStream input)
|
void |
setInput(String input)
Sets input for process. |
void |
setOutputTarget(OutputStream stdout)
Sets the OutputStream to which bytes written to the process' STDOUT will be directed. |
void |
setRedirectErrorStream(boolean redirect)
|
void |
setTimeout(long timeout,
TimeUnit timeoutUnit)
Sets timeout after which process will be stopped. |
void |
setWorkingDirectory(File dir)
Sets this process' working directory. |
void |
start()
Starts the new process to be monitored by this object. |
void |
unsetEnvironmentVariable(String key)
|
Integer |
waitFor()
Waits for the process to terminate, and returns the process' exit value. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public ProcessMonitor(String... command)
Constructs a monitored process with the specified operating system
program and arguments. It is not checked whether command
corresponds to a valid operating system command.
command - A string array containing the program and its arguments
NullPointerException - If the argument is nullpublic ProcessMonitor(List<String> command)
Constructs a monitored process with the specified operating system
program and arguments. It is not checked whether command
corresponds to a valid operating system command.
command - A list containing the program and its arguments
NullPointerException - If the argument is null| Method Detail |
|---|
public List<String> getCommand()
Returns this monitored process's operating system program and arguments. The returned list is a copy. Subsequent updates to the list will not be reflected in the state of this monitored process.
ProcessBuilder.command()public void setCommand(String... command)
Sets this monitored process's operating system program and arguments.
It is not checked whether command corresponds to a valid
operating system command.
command - A string array containing the program and its arguments
NullPointerException - If the argument is null
IllegalStateException - If this monitored process has been startedProcessBuilder.command(String...)public void setCommand(List<String> command)
Sets this monitored process's operating system program and arguments.
It is not checked whether command corresponds to a valid
operating system command.
command - A list containing the program and its argumentscommand -
NullPointerException - If the argument is null
IllegalStateException - If this monitored process has been startedProcessBuilder.command(List)public Map<String,String> getEnvironment()
public void setEnvironmentVariable(String key,
String value)
public void unsetEnvironmentVariable(String key)
public File getWorkingDirectory()
Returns this monitored process's working directory. The returned value
may be null -- this means to use the working directory of
the current Java process, usually the directory named by the system
property user.dir, as the working directory.
ProcessBuilder.directory()public void setWorkingDirectory(File dir)
Sets this process' working directory. The argument may be null -- this means to use the working directory of the current Java process, usually the directory named by the system property user.dir, as the working directory of the child process.
dir - The new working directory
IllegalStateException - If the process has been startedProcessBuilder.directory(File)public void setInput(byte[] input)
input -
NullPointerException - - if input is null.
IllegalStateException - - if process has been started, or input
is already set.public void setInput(String input)
input -
NullPointerException - - if input is null.public void setInput(InputStream input)
public void setErrorTarget(OutputStream stderr)
Sets the OutputStream to which bytes written to the process' STDERR will be directed. If this is set to null, then the output of the process will be discarded.
stderr -
IllegalStateException - If the process has been startedpublic void setOutputTarget(OutputStream stdout)
Sets the OutputStream to which bytes written to the process' STDOUT will be directed. If this is set to null, then the output of the process will be discarded.
stdout -
IllegalStateException - If the process has been startedpublic void setRedirectErrorStream(boolean redirect)
public boolean getRedirectErrorStream()
public void setTimeout(long timeout,
TimeUnit timeoutUnit)
timeout - - timeout in milliseconds.
IllegalStateException - - if thread has been started.public long getTimeout(TimeUnit unit)
public Integer getExitValue()
throws ExecutionException,
TimeoutException
IllegalStateException - If process has not been started, or is still running
ExecutionException - If this process threw an exception
TimeoutException - If this process timed out
CancellationException - If this process was cancelled
public int execute()
throws ExecutionException,
InterruptedException,
TimeoutException
InterruptedException
IllegalStateException - - If process already started
InterruptedException - If this thread was interrupted before
the process had finished.
TimeoutException
ExecutionException - If the process failed. This exception
contains the output up to the point of
failure, and the cause of failure.
public ProcessOutput runProcess()
throws ExecutionException,
InterruptedException,
TimeoutException
InterruptedException
IllegalStateException - - If process already started
InterruptedException - If this thread was interrupted before
the process had finished.
TimeoutException
ExecutionException - If the process failed. This exception
contains the output up to the point of
failure, and the cause of failure.
public Integer waitFor()
throws ExecutionException,
TimeoutException,
InterruptedException
Waits for the process to terminate, and returns the process' exit value.
IllegalStateException - If the process has not been started
ExecutionException - If an error occurs while the process is executing
CancellationException - If the process is cancelled
TimeoutException - If the process timed out
InterruptedException - If this thread is interrupted while waiting for the process to
finishpublic void start()
Starts the new process to be monitored by this object.
IllegalStateException - If the process has been started.public void cancel()
Cancel the process monitored by this object. If the process has already terminated, nothing is done.
IllegalStateException - If the process has not startedpublic byte[] getCachedOutput()
Returns an array containing the last bytes the process monitored by this object has written to STDOUT.
IllegalArgumentException - If the process has not startedpublic byte[] getCachedError()
Returns an array containing the last bytes the process monitored by this object has written to STDERR.
IllegalArgumentException - If the process has not startedpublic ProcessMonitor.ProcessState getState()
public Throwable getFailCause()
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||