Determine Version and Path of your Java Virtual Machine

When starting any Java applications you usually don’t need any knowledge about the underlying JVM and its installation path. But some days ago a collegue was confronted with a very delicate problem regarding the usage of the default truststores of different installed JVMs on a host machine that call each other. To find the truststore in charge we had to find the right JVM. So let’s sum up how to determine your JVMs in different cases:

1. Determine Path and Version of the actual default JVM used by the System

Open a console window and type:

>>> java -verbose -version

You will get something like:

[Loaded java.lang.Void from C:\Program Files\Java\jre1.8.0_31\lib\rt.jar]
[Loaded java.lang.IllegalAccessException from C:\Program Files\Java\jre1.8.0_31\lib\rt.jar]
[Loaded sun.misc.PostVMInitHook from C:\Program Files\Java\jre1.8.0_31\lib\rt.jar]
java version "1.8.0_31"
Java(TM) SE Runtime Environment (build 1.8.0_31-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.31-b07, mixed mode)
[Loaded java.lang.Shutdown from C:\Program Files\Java\jre1.8.0_31\lib\rt.jar]
[Loaded java.lang.Shutdown$Lock from C:\Program Files\Java\jre1.8.0_31\lib\rt.jar]

Its very important that you type the -verbose option first, otherwise you will only get the version information.

2. Determine the Version of any JVM on the System

Open a console window, navigate to the path of each JVM and type:

>>> ./java -version

You will get something like:

java version "1.8.0_31"
Java(TM) SE Runtime Environment (build 1.8.0_31-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.31-b07, mixed mode)

It’s very important to specify the current directory ./ if you are using the Windows Powershell or a Linux console window. If you don’t specify the current directory, the default JVM will be called and you will get the version of the default JVM instead of the JVM of the current directory.

3. Determine Path and Version of a JVM at Runtime

To get the the information at runtime, put the following code into the source code of your application or call this in debug mode:


System.getProperty("java.home");
System.getProperty("java.version");

4. Determine Path and Version of a running JVM or Processes

To gather the same information out of running processes that you can’t stop, for example running production servers, you can use the jps command (https://docs.oracle.com/javase/8/docs/technotes/tools/unix/jps.html) and jcmd utility (https://docs.oracle.com/javase/8/docs/technotes/tools/unix/jcmd.html). Both are part of the JDK and not part of simple JRE.

But before getting the path and version you first have to determine the process-ids of the java processes of your interest. Your first idea may be to call the tasklist command on Windows systems or ps command on Unix-like systems. But if you call them, you will get a meaningless list of processes like:


javaw.exe 11940 Console 1 61.400 K
javaw.exe 12484 Console 1 36.992 K
javaw.exe 7088 Console 1 714.736 K

Instead use the JPS command against a remote machine:

>>> jps VM001:1099

or the JCMD command to show the names of the running local processes:


>>> jcmd -l

If you do so, you will get a nice list of running processes:


11940 com.sun.jme.toolkit.bootstrap.Container
12484 com.sonyericsson.sdkme.proxy.GUI
8580 sun.tools.jcmd.JCmd -l
7088 C:\Program Files\eclipse-3.7.2\\plugins/org.eclipse.equinox.launcher_1.2.0.v20110502.jar -os win32 -ws win32 -arch x86_64 -showsplash -launcher C:\Program Files\eclipse-3.7.2\eclipse.exe -name Eclipse --launcher.library C:\Program Files\eclipse-3.7.2\\plugins/org.eclipse.equinox.launcher.win32.win32.x86_64_1.1.100.v20110502\eclipse_1406.dll -startup C:\Program Files\eclipse-3.7.2\\plugins/org.eclipse.equinox.launcher_1.2.0.v20110502.jar --launcher.overrideVmargs -exitdata 3328_104 -product org.eclipse.epp.package.reporting.product -vm C:/Program Files/Java/jdk1.7.0_21/bin/javaw.exe -vmargs -Dosgi.requiredJavaVersion=1.5 -Xms40m -Xmx512m -XX:MaxPermSize=256m -jar C:\Program Files\eclipse-3.7.2\\plugins/org.eclipse.equinox.launcher_1.2.0.v20110502.jar

Grab the PID of the process of your interest and put it into the jcmd command:

>>> jcmd 7088 VM.system_properties

You will get an overwhelming list of all system properties with version and path as well. If you are only interested in the version it’s more sufficient to take the option VM.version instead of VM.system_properties.

Don’t forget to…

… call all of these commands with the corresponding user who is running the processes. So if you want to investigate the processes of a usual Tomcat instance at Linux, first get the user who started up the processes:

>>> ps aux | grep 7088

Take the user and run your command with user rights:

>>> sudo -u tomcat8 jcmd 7088 VM.system_properties

A final note

Maybe you already read about the where command on Windows:

>>> where java
C:\ProgramData\Oracle\Java\javapath\java.exe
C:\Windows\System32\java.exe
C:\Program Files\Java\jdk1.6.0_33\bin\java.exe

And also you may read about the which command in Linux environments:

>>> which java
/usr/bin/java

Both commands only show the paths of registered java executables. If you have installed more than one JDK or JRE, you only get paths of the latest installed executables. So this can be a little bit missleading, but in some situations they may lead you to the default installation paths as well as described in the first bullet point.

Hinterlasse einen Kommentar

Diese Seite verwendet Akismet, um Spam zu reduzieren. Erfahre, wie deine Kommentardaten verarbeitet werden..