|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectbcds.tools.Exec
public class Exec
Executes an external program, waits for its completion and
redirects its standard output and standard error to user-provided
destinations. All IO exceptions are wrapped into unchecked
exceptions of type AnyException
.
The redirection is performed to a java.io.Writer instance, thus, if the caller knows that the output volume is small, he/she can can be use objects of type java.io.StringWriter.
Example:
{ Exec prg = new Exec("myprog", "-this", "-that", "myfile.txt"); prg.run(); // Another variant, with stdout and stderr combined. StringWriter out = new StringWriter(); Exec prg_redir = new Exec(out, out, "myprog", "-v", "myfile.txt"); prg_redir.run(); ... // A variant that does not require explicitinstantiation. String [] output = {"", ""}; Exec.run(output, "myprog", "-v", "myfile.txt"); System.out.printf("Out: %s\nErr: %s", output[0], output[1]); ... }
Constructor Summary | |
---|---|
Exec()
|
|
Exec(java.lang.String... cmd)
Creates an instance with stderr and stdout bound to System.out and System.err, respectively. |
|
Exec(java.io.Writer stdout,
java.io.Writer stderr,
java.lang.String... cmd)
Creates an instance with redirected output. |
Method Summary | |
---|---|
java.lang.String[] |
getCmd()
Returns the command that is instance executes. |
java.io.Writer |
getStdErr()
Returns the Writer to which stderr is redirected. |
java.io.Writer |
getStdOut()
Returns the Writer to which stdout is redirected. |
int |
run()
Runs the command and returns its exit code. |
static int |
run(java.lang.String[] output,
java.lang.String... cmd)
Executes the command given in cmd and returns the
command's stdout in output[0] and the command's
stderr in output[1]. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public Exec()
public Exec(java.lang.String... cmd)
cmd
must be non-null and non-empty.
public Exec(java.io.Writer stdout, java.io.Writer stderr, java.lang.String... cmd)
cmd
must be a non-null non-empty string. If stdout
is null,
cmd
' stdout is redirected to System.out.
If stderr
is null,
cmd
' stderr is redirected to System.err.
Method Detail |
---|
public java.io.Writer getStdOut()
public java.io.Writer getStdErr()
public java.lang.String[] getCmd()
public int run()
The command's stderr is always read after stdout's output has been sent to its destination. Thus, if stderr and stdout are combined, their contents will always be in the given sequence.
AnyException
- wrapping InterruptedIOException thrown
by Process.waitFor()
.
AnyException
- wrapping any IOException.public static int run(java.lang.String[] output, java.lang.String... cmd)
cmd
and returns the
command's stdout in output[0] and the command's
stderr in output[1].
The parameter output
must be an initialized array
of at least two elements.
The exceptions thrown are the same as run()
.
Example of use:
{ String [] output = {"", ""}; int exit_code = Exec.run(output, "myprog", "-p1", "-p2"); System.out.printf("Out:\n%s\nErr:\n%s\n", output[0], output[1]); ... }
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |