ProcessBuilder start() in Java

start(): This method is available in the java.lang.ProcessBuilder class of Java.

Syntax:

Process java.lang.ProcessBuilder.start() throws IOException

This 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().

Parameters: NA

Returns: a new Process object for managing the subprocess.

Throws:

1. NullPointerException - if an element of the command list is null.

2. IndexOutOfBoundsException - if the command is an empty list.

3. SecurityException - if a security manager exists and

a). its checkExecmethod doesn't allow creation of the subprocess.

b). the standard input to the subprocess was redirected from a file and the security manager's checkRead method denies read access to the file.

c). the standard output or standard error of thesubprocess was redirected to a fileand the security manager's checkWrite method denies write access to the file.

4. UnsupportedOperationException - If the operating system does not support the creation of processes.

5. IOException - if an I/O error occurs.

Approach 1: When no exception

Java

package com.ProcessBuilder;

import java.io.IOException;

public class ProcessBuilderstart {
    public static void main(String[] args) throws IOException {

        String commands[] = { "notepad.exe" };
        ProcessBuilder processBuilder =
new ProcessBuilder(commands);

        System.out.println(processBuilder.start());
    }
}

Output:

Process[pid=5704, exitValue="not exited"]


Approach 2: NullPointerException 

Java

package com.ProcessBuilder;

import java.io.IOException;

public class ProcessBuilderstart {
    public static void main(String[] args) throws IOException {

        String commands[] = null;
        ProcessBuilder processBuilder =
new ProcessBuilder(commands);

        System.out.println(processBuilder.start());
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot read the array length because "command" is null at java.base/java.lang.ProcessBuilder.<init>(ProcessBuilder.java:229) at com.ProcessBuilder.ProcessBuilderstart.main(ProcessBuilderstart.java:9)


Approach 3: IndexOutOfBoundsException

Java

package com.ProcessBuilder;

import java.io.IOException;

public class ProcessBuilderstart {
    public static void main(String[] args) throws IOException {

        String commands[] = {};
        ProcessBuilder processBuilder =
new ProcessBuilder(commands);

        System.out.println(processBuilder.start());
    }
}

Output:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0 at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1094) at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1073) at com.ProcessBuilder.ProcessBuilderstart.main(ProcessBuilderstart.java:11)


Approach 4: IOException 

Java

package com.ProcessBuilder;

import java.io.IOException;

public class ProcessBuilderstart {
    public static void main(String[] args) throws IOException {

        String commands[] = { "hello.exe" };
        ProcessBuilder processBuilder =
new ProcessBuilder(commands);

        System.out.println(processBuilder.start());
    }
}

Output:

Exception in thread "main" java.io.IOException: Cannot run program "hello.exe": CreateProcess error=2, The system cannot find the file specified at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1142) at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1073) at com.ProcessBuilder.ProcessBuilderstart.main(ProcessBuilderstart.java:11) Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified at java.base/java.lang.ProcessImpl.create(Native Method) at java.base/java.lang.ProcessImpl.<init>(ProcessImpl.java:483) at java.base/java.lang.ProcessImpl.start(ProcessImpl.java:158) at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1109) ... 2 more


Some methods of ProcessBuilder class

ProcessBuilder.ProcessBuilder(String...)This method constructs a process builder with the specified operating system program and arguments.

ProcessBuilder.ProcessBuilder(List<String>)This method constructs a process builder with the specified operating system program and arguments.

command()This method returns this process builder's operating system program and arguments.

command(List<String>)This method sets this process builder's operating system program and arguments.

command(String...)This method sets this process builder's operating system program and arguments. 

directory()This method returns this process builder's working directory.

directory(File)This method sets this process builder's working directory.

environment()This method returns a string map view of this process builder's environment.

inheritIO()This method sets the source and destination for subprocess standard I/O to be the same as those of the current Java process.

redirectError()This method returns this process builder's standard error destination.

redirectError(File)This method sets this process builder's standard error destination to a file.

redirectError(Redirect)This method sets this process builder's standard error destination.

redirectErrorStream()This method tells whether this process builder merges standard error and standard output.

redirectErrorStream(boolean)This method sets this process builder's redirectErrorStream property.

redirectInput()This method returns this process builder's standard input source.

redirectInput(File)This method sets this process builder's standard input source to a file.

redirectInput(Redirect)This method sets this process builder's standard input source.

redirectOutput()This method returns this process builder's standard output destination.

redirectOutput(File)This method sets this process builder's standard output destination to a file.

redirectOutput(Redirect)This method sets this process builder's standard output destination.

start()This method starts a new process using the attributes of this process builder.

ProcessBuilder.startPipeline(List<ProcessBuilder>)his method starts a Process for each ProcessBuilder, creating a pipeline of processes linked by their standard output and standard input streams.

No comments:

Post a Comment