Java ProcessBuilder redirect

Description

Java ProcessBuilder redirect


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. ja  v a 2 s. c  o m*/
    }

    // Configure the ProcessBuilder
    ProcessBuilder pb = new ProcessBuilder(javaPath, "--version")
        .redirectOutput(ProcessBuilder.Redirect.to(new File("java_product_details.txt")));
    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();
    }
  }
}



PreviousNext

Related