public class ExecRunner extends Object
Makes running external executables easier, optionally under a watched thread. In addition, probably the most useful feature of ExecRunner is using it to run a command-line program and obtain its stdout and stderr results in two strings. This is done with exec(String) - see that method for an example. With acknowledgements to Michael C. Daconta, author of "Java Pitfalls, Time Saving Solutions, and Workarounds to Improve Programs." and his article in JavaWorld "When Runtime.exec() Won't".
Constructor and Description |
---|
ExecRunner()
Basic ExecRunner constructor.
|
ExecRunner(String command)
ExecRunner constructor which also conveniently runs exec(String).
|
ExecRunner(String command,
String[] arguments)
ExecRunner constructor which also conveniently runs exec(String).
|
Modifier and Type | Method and Description |
---|---|
Object |
clone()
We override the
clone method here to prevent cloning of our class. |
int |
exec(String command)
The exec(String) method runs a process inside of a watched thread.
|
int |
exec(String command,
OutputStream stdoutStream,
OutputStream stderrStream)
Convenience method for calling exec with OutputStreams.
|
int |
exec(String command,
PrintWriter stdoutWriter,
PrintWriter stderrWriter)
The
exec(String, PrintWriter, PrintWriter) method runs
a process inside of a watched thread. |
int |
exec(String command,
String[] arguments)
Sometimes special cases may occur that the arguments of an external program
are Strings containing white spaces.
|
int |
exec(String command,
String[] arguments,
OutputStream stdoutStream,
OutputStream stderrStream)
Convenience method for calling exec with OutputStreams.
|
int |
exec(String command,
String[] arguments,
PrintWriter stdoutWriter,
PrintWriter stderrWriter)
The
exec(String, PrintWriter, PrintWriter) method runs
a process inside of a watched thread. |
String |
getErrString()
Returns the error string if exec(String) was invoked.
|
boolean |
getMaxRunTimeExceeded()
Returns whether the maximum runtime was exceeded or not.
|
int |
getMaxRunTimeSecs()
Returns the maximum run time in seconds for this object.
|
String |
getOutString()
Returns the output string if exec(String) was invoked.
|
static void |
main(String[] args)
This is for unit testing of the class.
|
void |
setMaxRunTimeSecs(int max)
Sets the maximum run time in seconds.
|
public ExecRunner()
public ExecRunner(String command) throws ExceptionInInitializerError
command
- The program or command to runExceptionInInitializerError
- thrown if a problem occurspublic ExecRunner(String command, String[] arguments) throws ExceptionInInitializerError
command
- The program or command to runarguments
- An array of Strings in which every single String
is exactly one of the program's arguments. So these arguments are
allowed to contain white spaces and are marked as Strings by the
system.ExceptionInInitializerError
- thrown if a problem occurspublic final Object clone() throws CloneNotSupportedException
clone
method here to prevent cloning of our class.clone
in class Object
CloneNotSupportedException
- To indicate cloning is not allowedpublic int exec(String command) throws IOException, InterruptedException
exec(program, arguments)should be used instead, where
arguments
is a String[]
array
containing the arguments of the program.
Example:
// Execute the program and grab the results String out = ""; String err = ""; try { ExecRunner er = new ExecRunner(); er.setMaxRunTimeSecs(maxRunTime); er.exec(program); if (!er.getMaxRunTimeExceeded()) { out = er.getOutString(); err = er.getErrString(); } else { log.error("Maximum run time exceeded!"); continue; } } catch (Exception e) { log.error("Error executing " + program + ": " + e.getMessage()); continue; }
command
- The program or command to runIOException
- thrown if a problem occursInterruptedException
- thrown if a problem occurspublic int exec(String command, String[] arguments) throws IOException, InterruptedException
command
- The external program to be executed.arguments
- An array of Strings. Every entry of this array is an argument
of the external program to be executed.IOException
- thrown if a problem occursInterruptedException
- thrown if a problem occurspublic int exec(String command, OutputStream stdoutStream, OutputStream stderrStream) throws IOException, InterruptedException
command
- The program or command to runstdoutStream
- java.io.OutputStreamstderrStream
- java.io.OutputStreamIOException
- thrown if a problem occursInterruptedException
- thrown if a problem occurspublic int exec(String command, String[] arguments, OutputStream stdoutStream, OutputStream stderrStream) throws IOException, InterruptedException
command
- The program or command to runarguments
- An array of Strings containing the program's arguments.stdoutStream
- java.io.OutputStreamstderrStream
- java.io.OutputStreamIOException
- thrown if a problem occursInterruptedException
- thrown if a problem occurspublic int exec(String command, PrintWriter stdoutWriter, PrintWriter stderrWriter) throws IOException, InterruptedException
exec(String, PrintWriter, PrintWriter)
method runs
a process inside of a watched thread. It returns the client's exit code
and feeds its STDOUT and STDERR to the passed-in streams.command
- The program or command to runstdoutWriter
- java.io.PrintWriterstderrWriter
- java.io.PrintWriterIOException
- thrown if a problem occursInterruptedException
- thrown if a problem occurspublic int exec(String command, String[] arguments, PrintWriter stdoutWriter, PrintWriter stderrWriter) throws IOException, InterruptedException
exec(String, PrintWriter, PrintWriter)
method runs
a process inside of a watched thread. It returns the client's exit code
and feeds its STDOUT and STDERR to the passed-in streams.
The arguments for the external program or command are passed as an array
of Strings. This has the advantage that the arguments are allowed to
contain white spaces and are marked to be Strings by the System itselfe,
which is sometimes important. Every single argument has to be exactly one
dimension of the arguments array.command
- The program or command to runarguments
- An array of Strings containing the program's arguments/options.stdoutWriter
- java.io.PrintWriterstderrWriter
- java.io.PrintWriterIOException
- thrown if a problem occursInterruptedException
- thrown if a problem occurspublic String getErrString()
public boolean getMaxRunTimeExceeded()
public int getMaxRunTimeSecs()
public String getOutString()
public static void main(String[] args) throws IOException
args
- an array of command-line argumentsIOException
- thrown if a problem occurspublic void setMaxRunTimeSecs(int max)
max
- Maximim number of seconds to let program runCopyright © 2014 BioJava. All rights reserved.