|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--com.mccrory.scott.base.ExecRunner
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 Summary | |
ExecRunner()
Basic ExecRunner constructor. |
|
ExecRunner(java.lang.String command)
ExecRunner constructor which also conveniently runs exec(String). |
Method Summary | |
java.lang.Object |
clone()
We override the clone method here to prevent cloning of our class. |
int |
exec(java.lang.String command)
The exec(String) method runs a process inside of a watched thread. |
int |
exec(java.lang.String command,
java.io.OutputStream stdoutStream,
java.io.OutputStream stderrStream)
Convenience method for calling exec with OutputStreams. |
int |
exec(java.lang.String command,
java.io.PrintWriter stdoutWriter,
java.io.PrintWriter stderrWriter)
The exec(String, PrintWriter, PrintWriter) method runs
a process inside of a watched thread. |
java.lang.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. |
java.lang.String |
getOutString()
Returns the output string if exec(String) was invoked. |
static void |
main(java.lang.String[] args)
This is for unit testing of the class. |
void |
setMaxRunTimeSecs(int max)
Sets the maximum run time in seconds. |
Methods inherited from class java.lang.Object |
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public ExecRunner()
public ExecRunner(java.lang.String command) throws java.lang.ExceptionInInitializerError
command
- The program or command to runjava.lang.ExceptionInInitializerError
- thrown if a problem occursMethod Detail |
public final java.lang.Object clone() throws java.lang.CloneNotSupportedException
clone
method here to prevent cloning of our class.
clone
in class java.lang.Object
java.lang.CloneNotSupportedException
- To indicate cloning is not allowedpublic int exec(java.lang.String command) throws java.io.IOException, java.lang.InterruptedException
// 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 runjava.io.IOException
- thrown if a problem occursjava.lang.InterruptedException
- thrown if a problem occurspublic int exec(java.lang.String command, java.io.OutputStream stdoutStream, java.io.OutputStream stderrStream) throws java.io.IOException, java.lang.InterruptedException
command
- The program or command to runstdoutStream
- java.io.OutputStreamstderrStream
- java.io.OutputStreamjava.io.IOException
- thrown if a problem occursjava.lang.InterruptedException
- thrown if a problem occurspublic int exec(java.lang.String command, java.io.PrintWriter stdoutWriter, java.io.PrintWriter stderrWriter) throws java.io.IOException, java.lang.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.PrintWriterjava.io.IOException
- thrown if a problem occursjava.lang.InterruptedException
- thrown if a problem occurspublic java.lang.String getErrString()
public boolean getMaxRunTimeExceeded()
public int getMaxRunTimeSecs()
public java.lang.String getOutString()
public static void main(java.lang.String[] args) throws java.io.IOException
args
- an array of command-line argumentsjava.io.IOException
- thrown if a problem occurspublic void setMaxRunTimeSecs(int max)
max
- Maximim number of seconds to let program run
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |