Before using this toolkit, I wrote the code to execute the Linux command, because I write code to execute the Linux returned information is not complete, because it is very simple to write the call is not used to wait for the return of information, and did not implement the expiration time, and then want to implement it, but inadvertently found the toolkit, I was excited when I was able to fulfill my needs.
Commons exec Download path
Version download 1.3 version can, in fact, only one version can be downloaded
1. Simple command execution
public static void Execlinuxcommand (String command) {
if (stringutils.isempty (command)) {
Log.info ("The executed Linux command cannot be empty");
return;
}
Defining Command-line objects
CommandLine CommandLine = new CommandLine (command);
try{
Defining Executor Objects
Executor Executor = new Defaultexecutor ();
If an exception occurs after the execution of the command, because the end identity value is not set
Executor.setexitvalue (1);
Executor.execute (CommandLine);
} catch (Exceptiion ex) {
Log.info ("Failed to execute Linux command", ex);
}
}
2. Command result information will be executed
try{
Defining execution result Information output
Bytearrayoutputstream ByteStream = new Bytearrayoutputstream ();
Define Expiration Time (Units/sec)
Executewatchdog watchdog = new Executewatchdog (3*1000);
Pumpstreamhandler Streamhandler = new Pumpstreamhandler (ByteStream);
Execute.setstreamhandler (Streamhandler);
Execute.setwatchdog (watchdog);
}catch (Exception ex) {
}
After defining Streamhandler, he built a method for printing
Use of the Apache Commons Exec Toolkit