public class Exec extends java.lang.Object implements Service
encapsulates a java.lang.Process
object, feeds it with
input and collects the output.
Example use:
Process p = new Process("your command here"); Exec exec = new Exec(p); if( !exec.done() ) { // do something with exec.getErrorText() // check p.exitValue() if the above is empty }
Constructor and Description |
---|
Exec(java.lang.Process p)
creates an execution environment for the running process
p which immediately closes its standard input and
collects standard output and standard error output
internally. |
Exec(java.lang.Process p,
java.io.InputStream in,
java.io.OutputStream output,
java.io.OutputStream error)
creates an execution environment which feeds the given process
input, collects its output and registers the exit code.
|
Modifier and Type | Method and Description |
---|---|
boolean |
done()
|
boolean |
finished()
asynchronuously tests if the process finished in the meantime.
|
java.lang.String |
getErrorText()
if no explicit output sink was specified in the constructor, this
method returns the output collected from the process.
|
java.lang.Exception |
getException()
returns whatever
Exception happened in one of the
Pipe threads used to copy data to the process or read
output or error from the process. |
java.lang.String |
getOutputText()
if no explicit error output sink was specified in the
constructor, this method returns the error text collected from
the process.
|
boolean |
ok()
returns
true if the process execution went
well. |
void |
run()
feeds the process with input and collects the output according to
the information given in the constructor.
|
public Exec(java.lang.Process p, java.io.InputStream in, java.io.OutputStream output, java.io.OutputStream error)
creates an execution environment which feeds the given process
input, collects its output and registers the exit code. You have
to call either run()
or done()
explicitly to get
things going.
p
- a running processin
- is the data source to feed into the standard input of
p
. May be null
in which case the standard
input of p
is immediately closed.output
- is the output sink for the standard output of
p
. It may be null
in which case the
output is collected internally and made available via getOutputText()
.error
- is the sink for the standard error output of
p
. If it is null
, it is collected and
made available via getErrorText()
.public Exec(java.lang.Process p)
p
which immediately closes its standard input and
collects standard output and standard error output
internally. The latter two can then be retrieved with getOutputText()
and it is null
, it is collected and
made available via getErrorText()
.
This is equivalent to Exec(p, null, null, null)
.
public boolean finished()
asynchronuously tests if the process finished in the meantime.
public boolean ok()
returns true
if the process execution went
well. The return value will be false
if either
getException()
),Note:If an error output sink
was explicitely given in the constructor, it is not watched to
see if any data is produced. In this case ok()
merely checks the exit value of the process and exceptions.
java.lang.IllegalStateException
- if the process has not finished
yet.public boolean done()
public java.lang.Exception getException()
returns whatever Exception
happened in one of the
Pipe
threads used to copy data to the process or read
output or error from the process. Since there are up to three
data streams connected to the process in parallel threads, it can
not be guaranteed that the first one failing will be
reported. Rather they are prioritized in the order stdin, stdout,
stderr.
getException
in interface Service
public java.lang.String getErrorText()
if no explicit output sink was specified in the constructor, this method returns the output collected from the process.
java.lang.IllegalStateException
- if the process has not finished
yet.public java.lang.String getOutputText()
if no explicit error output sink was specified in the constructor, this method returns the error text collected from the process.
java.lang.IllegalStateException
- if the process has not finished
yet.public void run()
feeds the process with input and collects the output according to
the information given in the constructor. The method will return
when the process has finished and all data produced by it was
read. In addition, run()
will kill the process and
return, if the thread it is running in is interrupted.
run
in interface java.lang.Runnable