|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectorg.opennms.netmgt.utils.ExecRunner
public class ExecRunner
Runs external executables, 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(String command)
ExecRunner constructor which also conveniently runs exec(String). |
|
| Method Summary | |
|---|---|
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. |
String |
getErrString()
Returns the error string if exec(String) was invoked. |
int |
getMaxRunTimeSecs()
Returns the maximum run time in seconds for this object. |
String |
getOutString()
Returns the output string if exec(String) was invoked. |
boolean |
isMaxRunTimeExceeded()
Returns whether the maximum runtime was exceeded or not. |
static void |
main(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(String command)
throws ExceptionInInitializerError
command - The program or command to run
ExceptionInInitializerError - thrown if a problem occurs| Method Detail |
|---|
public final Object clone()
throws CloneNotSupportedException
clone method here to prevent cloning of
our class.
clone in class ObjectCloneNotSupportedException - To indicate cloning is not allowed
public int exec(String command)
throws IOException,
InterruptedException
// Execute the program and grab the results
try {
ExecRunner er = new ExecRunner();
er.setMaxRunTimeSecs(5);
er.exec("ls -l");
if (!er.getMaxRunTimeExceeded()) {
out = er.getOutString();
err = er.getErrString();
} else {
System.out.println("Maximum run time exceeded!");
}
} catch (Exception e) {
System.out.println("Error executing " + program + ": " + e.getMessage());
continue;
}
command - The program or command to run
IOException - thrown if a problem occurs
InterruptedException - thrown if a problem occurs
public int exec(String command,
OutputStream stdoutStream,
OutputStream stderrStream)
throws IOException,
InterruptedException
command - The program or command to runstdoutStream - java.io.OutputStreamstderrStream - java.io.OutputStream
IOException - thrown if a problem occurs
InterruptedException - thrown if a problem occurs
public 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.PrintWriter
IOException - thrown if a problem occurs
InterruptedException - thrown if a problem occurspublic String getErrString()
public boolean isMaxRunTimeExceeded()
public int getMaxRunTimeSecs()
public String getOutString()
public static void main(String[] args)
throws IOException
args - an array of command-line arguments
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 | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||