Share this page 

Get the running JVM PathTag(s): Environment


JDK9
import java.io.IOException;
import java.util.Optional;

public class ProcessUtils {
  private ProcessUtils() {}

  public static Optional getCommandOfCurrentProcess() {
     ProcessHandle processHandle = ProcessHandle.current();
     return processHandle.info().command();
  }

  public static void main(String[] args) throws IOException {
    System.out.println(ProcessUtils.getCommandOfCurrentProcess());
  }
}

/*
  output:

  Optional[C:\Dev\Java\jdk-9\bin\javaw.exe]

*/
pre JDK9 (Win only)

We get the current PID (process identifier) and we launch a special VBScript to query process attached to the given PID. The method JVMUtils.getJVMPath() returns the JVM and its path used by the current process. The method JVMUtils.getJVMCommandLine() returns the JVM and its path plus the complete command line used by the current process.

Works only on Windows

package com.rgagnon.howto;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

public class JVMUtils {

  private JVMUtils() {}

  private static long getPID() {
    String processName =
      java.lang.management.ManagementFactory.getRuntimeMXBean().getName();
    return Long.parseLong(processName.split("@")[0]);
  }

  /**
   * @return the current JVM used by this process or null
   */
  public static String getJVMPath() {
    String path = "";
    BufferedReader input = null;
    try {
        File file = File.createTempFile("realhowto",".vbs");
        file.deleteOnExit();
        FileWriter fw = new java.io.FileWriter(file);

        String vbs = "Set WshShell = WScript.CreateObject(\"WScript.Shell\")\n"
                   + "Set locator = CreateObject(\"WbemScripting.SWbemLocator\")\n"
                   + "Set service = locator.ConnectServer()\n"
                   + "Set processes = service.ExecQuery _\n"
                   + " (\"select * from Win32_Process where ProcessId='"
                   + JVMUtils.getPID() +"'\")\n"
                   + "For Each process in processes\n"
                   + "wscript.echo process.ExecutablePath \n"
                   + "Next\n"
                   + "Set WSHShell = Nothing\n";
        fw.write(vbs);
        fw.close();
        Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
        input =
            new BufferedReader
              (new InputStreamReader(p.getInputStream()));
        path = input.readLine();
    }
    catch(Exception e){
        e.printStackTrace();
        return null;
    }
    finally {
      try { if (input != null) { input.close(); }  }
      catch (IOException io) { io.printStackTrace(); return null; }
    }
    return path;
  }

  /**
   * @return the JVM used by the current process and its command line or null
   */
  public static String getJVMCommandLine() {
    String path = "";
    BufferedReader input = null;
    try {
        File file = File.createTempFile("realhowto",".vbs");
        file.deleteOnExit();
        FileWriter fw = new java.io.FileWriter(file);

        String vbs = "Set WshShell = WScript.CreateObject(\"WScript.Shell\")\n"
                   + "Set locator = CreateObject(\"WbemScripting.SWbemLocator\")\n"
                   + "Set service = locator.ConnectServer()\n"
                   + "Set processes = service.ExecQuery _\n"
                   + " (\"select * from Win32_Process where ProcessId='"
                   + JVMUtils.getPID() +"'\")\n"
                   + "For Each process in processes\n"
                   + "wscript.echo process.CommandLine \n"
                   + "Next\n"
                   + "Set WSHShell = Nothing\n";
        fw.write(vbs);
        fw.close();
        Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
        input =
            new BufferedReader
              (new InputStreamReader(p.getInputStream()));
        path = input.readLine();
    }
    catch(Exception e){
        e.printStackTrace();
        return null;
    }
    finally {
      try { if (input != null) { input.close(); }  }
      catch (IOException io) { io.printStackTrace(); return null; }
    }
    return path;
  }


  public static void main(String[] args) {
    System.out.println(JVMUtils.getJVMPath());
    /*
    output (example):
    C:\Program Files (x86)\Java\jre6\bin\javaw.exe
    */
    System.out.println(JVMUtils.getJVMCommandLine());
    /*
     output (example):
     "C:\Program Files (x86)\Java\jre7\bin\javaw.exe" -Dfile.encoding=Cp1252 -classpath "C:\Applications\dev\eclipseworkspace\Howto\bin;" com.rgagnon.howto.JVMUtils
    */
  }
}

See also Determine if running from JAR, Get the "root" of an application and Obtain from where a Class is loaded