Java ProcessBuilder redirect input, output and error
import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) { // Get the path of the java program that started this program String javaPath = ProcessHandle.current().info().command().orElse(null); if (javaPath == null) { System.out.println("Could not get the java command's path."); return;//from w w w. j a v a 2 s. c o m } // Configure the ProcessBuilder inheriting parent's I/O ProcessBuilder pb = new ProcessBuilder(javaPath, "--version") .redirectInput(ProcessBuilder.Redirect.INHERIT) .redirectOutput(ProcessBuilder.Redirect.INHERIT) .redirectError(ProcessBuilder.Redirect.INHERIT); try { // Start a new java process Process p = pb.start(); // Read and print the standard output stream of the process try (BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()))) { String line; while ((line = input.readLine()) != null) { System.out.println(line); } } } catch (IOException e) { e.printStackTrace(); } } }