Javatpoint Logo
Javatpoint Logo

ProcessBuilder in Java

The class ProcessBuilder is used to create the operating system process in Java. The collection of process attributes is managed by each instance of the ProcessBuilder class. The ProcessBuilder class cannot be synchronized. Prior to JDK 5.0, it was the Runtime.exec() method that was used to create the process and execute it. Note that the ProcessBuilder class extends the Object class.

Constructors of The ProcessBuilder Class

ProcessBuilder(List command): It does the construction of the process builder with the mentioned arguments and operating system program.

ProcessBuilder(String… command): It also does the construction of the process builder with the mentioned arguments and operating system program.

Methods of The ProcessBuilder Class

1. List Command(): The method returns the arguments and the process builder's operating system program.

Syntax: public List command().

Returns: the arguments and the process builder's program.

Exceptions: Gives the NullPointerException whenever the argument is null.

FileName: ProcessBuilderDemo.java

Output:

command: [notepad++.exe]

2. ProcessBuilder directory(File directory): The method sets the working directory of the process builder. The subprocesses invoked by the start() method utilize it as the working directory.

Syntax: public ProcessBuilder directory(File directory).

Returns: The process builder.

Exception: NA.

FileName: ProcessBuilderDemo1.java

Output:

directory: src

3. Map environment(): The string map view of the environment of the process builder's is returned by this method. The environment gets initialized with a copy of the current process environment when the process builder is created.

Syntax: public Map environment()

Returns: The environment of the process builder's.

Exception: SecurityException - if a security manager is in existence and its method checkPermission() does not give permission to access the process environment.

FileName: ProcessBuilderDemo2.java

Output:

Key = USERDOMAIN_ROAMINGPROFILE, Value = DESKTOP-2CFH9HQ
Key = LOCALAPPDATA, Value = C:\Users\Nikhil\AppData\Local
Key = PROCESSOR_LEVEL, Value = 6
Key = USERDOMAIN, Value = DESKTOP-2CFH9HQ
Key = FPS_BROWSER_APP_PROFILE_STRING, Value = Internet Explorer
Key = LOGONSERVER, Value = \\DESKTOP-2CFH9HQ
Key = PROMPT, Value = $P$G
Key = SESSIONNAME, Value = Console
Key = ALLUSERSPROFILE, Value = C:\ProgramData
Key = =K:, Value = K:\
Key = PROCESSOR_ARCHITECTURE, Value = AMD64
Key = PSModulePath, Value = C:\Windows\system32\WindowsPowerShell\v1.0\Modules\
Key = SystemDrive, Value = C:
Key = =ExitCode, Value = 00000000
Key = OneDrive, Value = C:\Users\Nikhil\OneDrive
Key = APPDATA, Value = C:\Users\Nikhil\AppData\Roaming
Key = USERNAME, Value = Nikhil
Key = ProgramFiles(x86), Value = C:\Program Files (x86)
Key = CommonProgramFiles, Value = C:\Program Files\Common Files
Key = Path, Value = C:\Program Files\Common Files\Oracle\Java\javapath;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Java\jdk-11.0.13\bin
Key = FPS_BROWSER_USER_PROFILE_STRING, Value = Default
Key = PATHEXT, Value = .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
Key = OS, Value = Windows_NT
Key = COMPUTERNAME, Value = DESKTOP-2CFH9HQ
Key = PROCESSOR_REVISION, Value = 8e09
Key = CommonProgramW6432, Value = C:\Program Files\Common Files
Key = ComSpec, Value = C:\Windows\system32\cmd.exe
Key = ProgramData, Value = C:\ProgramData
Key = ProgramW6432, Value = C:\Program Files
Key = HOMEPATH, Value = \Users\Nikhil
Key = SystemRoot, Value = C:\Windows
Key = TEMP, Value = C:\Users\Nikhil\AppData\Local\Temp
Key = HOMEDRIVE, Value = C:
Key = PROCESSOR_IDENTIFIER, Value = Intel64 Family 6 Model 142 Stepping 9, GenuineIntel
Key = USERPROFILE, Value = C:\Users\Nikhil
Key = TMP, Value = C:\Users\Nikhil\AppData\Local\Temp
Key = CommonProgramFiles(x86), Value = C:\Program Files (x86)\Common Files
Key = ProgramFiles, Value = C:\Program Files
Key = PUBLIC, Value = C:\Users\Public
Key = NUMBER_OF_PROCESSORS, Value = 4
Key = windir, Value = C:\Windows
Key = =::, Value = ::\

Note: The above output is the dynamic one. It is because the output varies from one user to other user depending on the system the user is using.

4. boolean redirectErrorStream(): The method returns a Boolean value showing whether the process builder merges standard output and standard error. If the method redirectErrorStream() returns true, then any error output generated by the subprocesses subsequently initiated by the start() method gets merged with the standard output so that both can be read using the Process.getInputStream() method. The method makes it simpler to do the correlation of the error messages with the corresponding output. Note that the beginning value is false for the property redirectErrorStream.

Syntax: public boolean redirectErrorStream()

Returns: A Boolean value.

Exception: NA.

FileName: ProcessBuilderDemo3.java

Output:

false

5. ProcessBuilder redirectErrorStream(boolean redirectErrorStream): The method assigns the redirectErrorStream property of the process builder's. If the property redirectErrorStream is true, then any error produced by the subsequent subprocesses begun by the start() method gets merged with the standard. The initial value is false.

Syntax: public boolean redirectErrorStream(boolean redirectErrorStream)

Returns: A Boolean value.

Exception: NA.

FileName: ProcessBuilderDemo4.java

Output:

true

6. Process start(): The method begins a new process with the help of the attributes of the process builder. The new process invokes the arguments and command provided by command(), in the working directory provided by directory(), with the process environment provided by the environment(). The method checks whether the command compatible with the operating system or not.

Syntax: public Process start().

Returns: a new Process object for managing the subprocess.

Exception:

  • NullPointerException - If an element of the command list is null.
  • IndexOutOfBoundsException - If the command is an empty list (has size 0).
  • SecurityException - If a security manager exists and its checkExec method does not allow the creation of the subprocess. IOException - If an I/O error occurs.

FileName: ProcessBuilderDemo5.java

Output:

Volume in drive C has no label.
Volume Serial Number is E44F-AA1A

 Directory of C:\Users\Nikhil\Downloads\March Month Content

04/02/2022  12:53 PM    <DIR>          .
04/02/2022  12:53 PM    <DIR>          ..
03/07/2022  02:16 AM            72,341 Arithmetic Exception in Java.docx
04/01/2022  07:50 PM            75,092 Balanced Prime in Java.docx
03/04/2022  12:06 AM            72,984 Comparetoignorecase Java.docx
03/07/2022  02:16 AM            67,015 Difference between ArrayList and LinkedList.docx
03/18/2022  10:29 PM            88,850 Display the nodes at odd levels of a tree.docx
04/01/2022  06:49 PM            68,855 Empty Statement in Java.docx
03/07/2022  02:13 AM            75,014 Iccanobif Numbers in Java.docx
03/22/2022  05:43 PM            82,319 Java HashSet.docx
03/29/2022  06:43 PM            77,242 Java LinkedHashSet class.docx
03/26/2022  05:06 AM            87,274 Maximum Rectangular Area in a Histogram.docx
04/01/2022  06:59 PM            75,088 Minimum Coins for Making a Given Value in Java.docx
03/07/2022  02:27 AM            77,622 Minimum Xor Value Pair in Java.docx
03/18/2022  10:24 PM            70,371 Nude Numbers in Java.docx
03/03/2022  01:21 AM            69,562 Trimorphic Numbers in Java.docx
03/28/2022  06:23 PM            86,764 Two Sorted LinkedList Intersection in Java.docx
              15 File(s)      1,146,393 bytes
               2 Dir(s)  13,788,762,112 bytes free

6. ProcessBuilder inheritIO(): Sets the destination and source for standard I/O subprocess same for the current Java process.

Syntax: public ProcessBuilder inheritIO().

Returns: The process builder.

Exception: NA.

FileName: myBat.bat

FileName: ProcessBuilderDemo6.java

Output:

"Welcome To JavaTpoint. A great site to Learn Java.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA