- Java.lang Package classes
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package extras
- Java.lang - Interfaces
- Java.lang - Errors
- Java.lang - Exceptions
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java ProcessBuilder start() Method
Description
The Java ProcessBuilder start() method starts a new process using the attributes of this process builder. The new process will invoke the command and arguments given by command(), in a working directory as given by directory(), with a process environment as given by environment(). This method checks that the command is a valid operating system command. Which commands are valid is system-dependent, but at the very least the command must be a non-empty list of non-null strings.
If there is a security manager, its checkExec method is called with the first component of this object's command array as its argument. This may result in a SecurityException being thrown.
Declaration
Following is the declaration for java.lang.ProcessBuilder.start() method
public Process start()
Parameters
NA
Return Value
This method 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 doesn't allow creation of the subprocess
IOException − If an I/O error occurs
Starting a Notepad process using Process Builder Example
The following example shows the usage of ProcessBuilder start() method. In this program, we've created an array of Strings and added notepad.exe and test.txt to it. Using that list, we've initialized a ProcessBuilder instance. Now using start() method, we've started the process.
package com.tutorialspoint; import java.io.IOException; public class ProcessBuilderDemo { public static void main(String[] args) { // create a new list of arguments for our process String[] list = {"notepad.exe", "test.txt"}; // create the process builder ProcessBuilder pb = new ProcessBuilder(list); try { // start the subprocess System.out.println("Starting the process.."); pb.start(); } catch (IOException ex) { ex.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result −
Starting the process..
Starting a Calculator process using Process Builder Example
The following example shows the usage of ProcessBuilder start() method. In this program, we've created an array of Strings and added calc.exe. Using that list, we've initialized a ProcessBuilder instance. Now using start() method, we've started the process.
package com.tutorialspoint; import java.io.IOException; public class ProcessBuilderDemo { public static void main(String[] args) { // create a new list of arguments for our process String[] list = {"calc.exe"}; // create the process builder ProcessBuilder pb = new ProcessBuilder(list); try { // start the subprocess System.out.println("Starting the process.."); pb.start(); } catch (IOException ex) { ex.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result −
Starting the process..
Starting a Windows Explorer process using Process Builder Example
The following example shows the usage of ProcessBuilder start() method. In this program, we've created an array of Strings and added explorer.exe. Using that list, we've initialized a ProcessBuilder instance. Now using start() method, we've started the process.
package com.tutorialspoint; import java.io.IOException; public class ProcessBuilderDemo { public static void main(String[] args) { // create a new list of arguments for our process String[] list = {"explorer.exe"}; // create the process builder ProcessBuilder pb = new ProcessBuilder(list); try { // start the subprocess System.out.println("Starting the process.."); pb.start(); } catch (IOException ex) { ex.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result −
Starting the process..