In-depth study of java. lang. ProcessBuilder class

Source: Internet
Author: User

I. Overview
The ProcessBuilder class is a new class added by J2SE 1.5 in java. lang. This class is used to create operating system processes. It provides a method to start and manage processes (that is, applications. Before J2SE 1.5, processes were controlled and managed by the Process class.
Each ProcessBuilder instance manages a process property set. Its start () method uses these attributes to create a new Process instance. The start () method can be called repeatedly from the same instance to create a new sub-process with the same or related attributes. (I have discussed this in the " java.lang.runtime》class. The progress can also be started by runtime.exe c .)
 
Each process Builder (that is, the ProcessBuilder object) manages these process attributes:
A command is a string list that indicates the external program file to be called and its parameters (if any ). Here, the list of valid operating system commands depends on the system. For example, every population variable usually needs to be an element in this list, but some operating systems want the program to mark the command line string themselves-in this system, java implementation may require commands to exactly include these two elements.
Environment is system-dependent ing from variable to value. The initial value is a copy of the current process environment (see System. getenv ()).
Working directory. The default value is the current working directory of the current process, which is usually named according to the system property user. dir.
RedirectErrorStream attribute. Initially, this attribute is false, meaning that the standard output and error output of the sub-Process are sent to two independent streams, which can be passed through the Process. getInputStream () and Process. the getErrorStream () method. If the value is set to true, the standard error is merged with the standard output. This makes it easier to associate error messages with the corresponding output. In this case, the merged data can be read from the stream returned by Process. getInputStream (), and the read from the stream returned by Process. getErrorStream () will directly reach the end of the file.
 
Since there is a Process class, why do we need to invent a ProcessBuilder class? What is the difference between ProcessBuilder and Process classes?
Originally, ProcessBuilder provided more control for the process. For example, you can set the current working directory or change the environment parameters. The Process function is much simpler.
ProcessBuilder is a final class that has two constructor methods with parameters. You can create a ProcessBuilder object directly by constructor. Processis an abstract class. Generally, runtime.exe c () and ProcessBuilder. start () are used to indirectly create an instance.
 
Note:
Modifying the attributes of the Process builder will affect the subsequent processes started by the start () method of the object, but it will never affect the previously started processes or Java processes.
The ProcessBuilder class is not synchronous. If multiple threads access a ProcessBuilder at the same time, and at least one thread modifies one of the attributes from the structure, it must maintain external synchronization.
 
It is easy to start a new process that uses the default working directory and environment:
Process p = new ProcessBuilder ("myCommand", "myArg"). start ();
The following is an example of starting a process using a modified working directory and environment:
ProcessBuilder pb = new ProcessBuilder ("myCommand", "myArg1", "myArg2 ");
Map <String, String> env = pb. environment ();
Env. put ("VAR1", "myValue ");
Env. remove ("OTHERVAR ");
Env. put ("VAR2", env. get ("VAR1") + "suffix ");
Pb. directory ("myDir ");
Process p = pb. start ();
To start a process with a set of clear environment variables, call Map. clear () before adding environment variables ().
 
Ii. API Preview
Constructor Summary
ProcessBuilder (List <String> command)
Constructs a process generator using the specified operating system program and parameters.
ProcessBuilder (String... command)
Constructs a process generator using the specified operating system program and parameters.
 
Method Summary
Command ()
Returns the operating system program and parameters of the Process Generator.
Command (List <String> command)
Set the operating system programs and parameters of the Process Generator.
Command (String... command)
Set the operating system programs and parameters of the Process Generator.
Directory ()
Returns the working directory of the Process Generator.
Directory (File directory)
Set the working directory of the Process Generator.
Environment ()
Returns the string ing view of the Process Generator environment.
RedirectErrorStream ()
Indicates whether the Process Generator combines standard errors and standard output.
RedirectErrorStream (boolean redirectErrorStream)
Set the redirectErrorStream attribute of this Process Generator.
Start ()
Use the properties of this Process Generator to start a new process.
 
Iii. common applications
To create a process using ProcessBuilder, you only need to create an instance of ProcessBuilder and specify the name and required parameters of the process. To execute this program, call start () on the instance. The following is an example of running Windows notepad. Note that the file name to be edited is specified as a parameter.
Class PBDemo {
Public static void main (String args []) {
Try {
ProcessBuilder proc = new ProcessBuilder ("notepad.exe", "testfile ");
Proc. start ();
} Catch (Exception e ){
System. out. println ("Error executing notepad .");
}
}
}

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.