ProcessBuilder.inheritIO() in Java 7
The ProcessBuilder class was updated in Java 7 to allow sub-processes to inherit the standard input, output, and error streams of the parent Java process.
How it Works
Using inheritIO(), the child process's output is redirected directly to the parent process's console. This eliminates the need to manually read from the process's InputStream to see its output.
Java Example
import java.io.IOException;
public class ProcessInheritExample {
public static void main(String[] args) throws IOException, InterruptedException {
ProcessBuilder pb = new ProcessBuilder("cmd", "/c", "echo Hello");
pb.inheritIO();
pb.start().waitFor();
}
}
Output
Hello
Key Points
- Directly link child and parent I/O
- Simplifies command-line tool integration
- No need for manual stream redirection
- Improves performance by avoiding intermediate buffers
Comments