ProcessBuilder.startPipeline(List<ProcessBuilder>): This method is available in the java.lang.ProcessBuilder class of Java.
Syntax:
List<Process> java.lang.ProcessBuilder.startPipeline(List<ProcessBuilder> builders) throws IOException
This method takes one argument. This method starts a Process for each ProcessBuilder, creating a pipeline of processes linked by their standard output and standard input streams.
Parameters: One parameter is required for this method.
builders: a List of ProcessBuilders.
Returns: a List<Process> started from the corresponding ProcessBuilder.
Throws:
1. IllegalArgumentException - any of the redirects except the standard input of the first builder and the standard output of the last builder are not Redirect.PIPE.
2. NullPointerException - if an element of the command list is null or if an element of the ProcessBuilder list is null or the builders argument is null.
3. IndexOutOfBoundsException - if the command is an empty list.
4. 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 the subprocess was redirected to a file and the security manager's checkWrite method denies write access to the file.
5. UnsupportedOperationException - If the operating system does not support the creation of processes.
6. IOException - if an I/O error occurs.
Approach 1: When no exception
Java
package com.ProcessBuilder;import java.io.IOException;import java.util.ArrayList;import java.util.List;public class ProcessBuilderstartPipeline {public static void main(String[] args) throws IOException {ProcessBuilder processBuilder =new ProcessBuilder("notepad.exe");ProcessBuilder processBuilder2 =new ProcessBuilder("notepad.exe");List<ProcessBuilder> list =new ArrayList<ProcessBuilder>();list.add(processBuilder);list.add(processBuilder2);System.out.println(ProcessBuilder.startPipeline(list));}}
Output:
[Process[pid=8000, exitValue="not exited"], Process[pid=12080, exitValue="not exited"]]
Approach 2: NullPointerException
Java
package com.ProcessBuilder;import java.io.IOException;import java.util.List;public class ProcessBuilderstartPipeline {public static void main(String[] args) throws IOException {List<ProcessBuilder> list = null;System.out.println(ProcessBuilder.startPipeline(list));}}
Output:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.List.size()" because "builders" is null at java.base/java.lang.ProcessBuilder.startPipeline(ProcessBuilder.java:1272) at com.ProcessBuilder.ProcessBuilderstartPipeline.main(ProcessBuilderstartPipeline.java:11)
Approach 3: IOException
Java
package com.ProcessBuilder;import java.io.IOException;import java.util.ArrayList;import java.util.List;public class ProcessBuilderstartPipeline {public static void main(String[] args) throws IOException {ProcessBuilder processBuilder =new ProcessBuilder("notepad.exe");ProcessBuilder processBuilder2 =new ProcessBuilder("hello.exe");List<ProcessBuilder> list =new ArrayList<ProcessBuilder>();list.add(processBuilder);list.add(processBuilder2);System.out.println(ProcessBuilder.startPipeline(list));}}
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.startPipeline(ProcessBuilder.java:1297) at com.ProcessBuilder.ProcessBuilderstartPipeline.main(ProcessBuilderstartPipeline.java:18) 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