Java ProcessHandle get current

Description

Java ProcessHandle get current

import java.io.BufferedReader;
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 ww  w .j  a v a 2  s.c om
    }
    // Configure the ProcessBuilder
    ProcessBuilder pb = new ProcessBuilder(javaPath, "--version");

    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