Use of the Apache Commons Exec Toolkit

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.