Java Processbuilder Make Program Continue if Command Fails
Java ProcessBuilder Example
The Java.lang.ProcessBuilder class is one of the most important classes that is used for creating OS(Operating System) processes. A set of process attributes are managed by each ProcessBuilder instance. The ProcessBuilder class provides the start() method for creating an instance of a new process with those process attributes.
We can repeatedly invoke the start() method by using the same instance of the ProcessBuilder class for creating new subprocesses with the same or related attributes.
The ProcessBuilder class provide the following two constructors:
ProcessBuilder(List command)
The constructor creates an instance of the process builder with the given OS program and arguments.
ProcessBuilder(String… command)
The constructor creates an instance of the process builder with the given OS program and arguments.
The ProcessBuilder class provide the following methods, which are as follows:
List<String> command()
The command() method is used for getting the process builder's operating system program and arguments.
Syntax
Returns
It returns the current process builder's program and its arguments.
Exceptions
It may throw NullPointerException when the argument is null.
Let's take an example to understand how we can use the command() method of ProcessBuilder in Java.
ProcessBuilderExample1.java
Output:
ProcessBuilder command(List<String> command)
The command() method is used for setting the current process builder's operating system program and arguments.
Syntax
Parameters
It accepts a list of strings that contain the program and its arguments.
Returns
NA.
Exceptions
It may throw NullPointerException when the argument is null.
Let's take an example to understand how we can use the command() method of process builder in Java.
ProcessBuilderExample2.java
Output:
ProcessBuilder directory (File directory)
The directory() method is another important method of ProcessBuilder that is used to set the current process builder's directory.
The user-given directory is used as a working directory by the sub-processes subsequently started by the start() method of the object.
Syntax
Parameters
It accepts the new working directory.
Returns
It returns the current process builder.
Let's take an example to understand how we can use the directory() method of ProcessBuilder in Java.
ProcessBuilderExample3.java
Output:
Map environment()
It returns a String map that defines the view of the process builder's environment. The environment is initialized to a copy of the current process environment when the process builder is created. The returned map of the environment() method is used as a working directory by the sub-processes, subsequently started by the start() method of the object.
Syntax
Returns
It returns the environment of the current process builder.
Exception
It may throw the SecurityException when the security manager exists, and its checkPermission method doesn't allow access to the process.
Let's take an example to understand how we can use the environment() method of ProcessBuilder in Java.
ProcessBuilderExample4.java
Output:
ProcessBuilder redirectErrorStream(boolean redirectErrorStream)
The redirectErrorStream() method is used for setting the current process builder's redirectErrorStream property. If this property is true, any error output generated by subprocesses subsequently started by this object's start() method will be merged with the standard output so that both can be read using the process.getInputStream() method. This makes it easier to correlate error messages with the corresponding output. The initial value is false.
Syntax
Returns
It returns the current process builder.
Exception
NA.
ProcessBuilderExample5.java
Output:
Process start()
The start() method of ProcessBuilder is used to start a new process by using the attribute of the process builder. The new process will invoke the command and arguments given by command() in a working directory as given by directory(), with an environment as given by environment().
Syntax
Returns
A new Process object to manage the sub-process.
Exception
The start() method may throw one of the following exceptions:
- It may throw NullPointerException when an element of the command list is null.
- It may throw IndexOutOfBoundsException when the command is an empty list.
- It may throw SecurityException when a security manager exists, and its checkExec methods don't allow the creation of a subprocess.
- It may throw IOException when an I/O error occurs.
ProcessBuilderExample6.java
Output:
ProcessBuilder inheritIO()
The inheritIO() method is used to set the source and destination to the subprocess's standard I/O to be the same as those of the current java process.
Syntax
Returns
Current process builder
Exception
NA.
ProcessBuilderExample7.java
Output:
Source: https://www.javatpoint.com/java-processbuilder-example