redirectInput(Redirect): This method is available in the java.lang.ProcessBuilder class of Java.
Syntax:
ProcessBuilder java.lang.ProcessBuilder.redirectInput(Redirect source)
This method takes one argument. This method sets this process builder's standard input source.Subprocesses subsequently started by this object's start() method obtain their standard input from this source.
Parameters: One parameter is required for this method.
source: the new standard input source.
Returns: this process builder.
Throws:
1. IllegalArgumentException - if the redirect does not correspond to a valid source of data, that is, has type WRITE or APPEND.
Approach 1: When no exception
Java
package com.ProcessBuilder;import java.lang.ProcessBuilder.Redirect;public class ProcessBuilderredirectInput3 {public static void main(String[] args) {ProcessBuilder processBuilder =new ProcessBuilder("notepad.exe");Redirect source = Redirect.PIPE;System.out.println(processBuilder.redirectInput(source));}}
Output:
java.lang.ProcessBuilder@4926097b
Approach 2: IllegalArgumentException
Java
package com.ProcessBuilder;import java.lang.ProcessBuilder.Redirect;public class ProcessBuilderredirectInput3 {public static void main(String[] args) {ProcessBuilder processBuilder =new ProcessBuilder("notepad.exe");Redirect source = Redirect.DISCARD;System.out.println(processBuilder.redirectInput(source));}}
Output:
Exception in thread "main" java.lang.IllegalArgumentException: Redirect invalid for reading: WRITE at java.base/java.lang.ProcessBuilder.redirectInput(ProcessBuilder.java:764) at com.ProcessBuilder.ProcessBuilderredirectInput3.main(ProcessBuilderredirectInput3.java:12)
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