Teach you to execute other programs in Java

Source: Internet
Author: User
Tags constructor

When writing Java programs, you sometimes need to execute another program in a Java program.

1, startup program Java provides two ways to start other programs:

(1) the Exec () method using runtime

(2) using the Processbuilder start () method

Regardless of the operating system, the program has some essentially similar properties. A program after the start of the operating system of a process, the process at the time of execution has its own environment variables, have their own working directory. Runtime and Processbuilder provide different ways to start programs, set startup parameters, environment variables, and working directories.

An external program that can be executed in Java must be an actual executable file that cannot be executed directly for inline commands under the shell.

When using a runtime exec execution program, you first use the runtime static method to get a runtime, and then call the runtime Exec method. External programs and startup parameters, environment variables, and working directories that you want to execute are passed as arguments to the Exec method, which returns the program that a process representative executes.

Runtime has six exec methods, of which two are defined as:

Public Process exec (string[] cmdarray, string[] envp, File dir)

Public Process exec (String command, string[] envp, File dir)

Cmdarray and command for the commands to execute, you can pass commands and parameters as a string command to the Exec () method, or you can pass the command and parameter one by one to the Exec () method in the array Cmdarray.

ENVP is an environment variable, placed in an array in name=value form. Dir is the working directory.

You can leave the dir parameter, or not the envp and dir parameters, so that there are 4 more exec () methods. If there is no dir parameter or NULL, the newly initiated process inherits the working directory of the current Java process. If there is no ENVP parameter or NULL, the newly initiated process inherits the environment variables of the current Java process.

You can also use the Processbuilder class to start a new program that is later added to the JDK and is recommended for use. You set the command and the parameters you want to execute by using the constructor, or you can get the command information through the commands () method. Set up working directories through the directory (File directory) method to modify environment variables by environment () to obtain environment variable information.

After you use the Processbuilder constructor to create a new instance, set the environment variable, the working directory, you can start the new program by using the Start () method, as in the runtime exec () method, which returns a Process object that represents the program that was started.

The difference between the Processbuilder and the Runtime.exec () method is that Processbuilder provides a Redirecterrorstream (Boolean Redirecterrorstream) method, This method is used to redirect the error output of the process to the standard output. That is, the error output can be merged with the standard output.

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.