Apache Common exec executes external commands

Source: Internet
Author: User

You need to use Java to call external commands (shell scripts, start services, and so on) before using Runtime.getruntime (). Exec calls external programs, Runtime.getruntime (). Exec is the Java Primitive command, While the Apache commons-exec encapsulates some common methods used to execute external commands. For example, we want the file information under the current Windows directory, and the command under the cmd command is dir. The code example shows the 2 method implementations.

The first type of Runtime.getruntime (). exec

        String pl_cmd = "cmd.exe/c dir";         = runtime.getruntime (). exec (pl_cmd);         New BufferedReader (new  InputStreamReader (                p_pl.getinputstream ()));         = br_pl.readline ();          while NULL ) {            System.out.println (stdout);             = br_pl.readline ();        }

The second type of Apache Commons-exec

        New Bytearrayoutputstream ();         New Pumpstreamhandler (stdout);         = Commandline.parse ("cmd.exe/c dir");         New defaultexecutor ();        Exec.setstreamhandler (PSH);        Exec.execute (CL);        System.out.println (Stdout.tostring ());

Code reference GitHub

Apache Common exec executes external commands

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.