144

I want to write a sample Java file in which I want to know the JVM version in which the class is running. Is there a way?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
java_geek
  • 17,585
  • 30
  • 91
  • 113
  • 3
    few notes. System properties are meant for that but DO keep in mind it's a privileged operation and applets/webstart/sand boxed code will not be able to execute it (getting SecurityException). Normally you'd like to run it in similar way `AccessController.doPrivileged(new PrivilegedAction(...)); ` – bestsss Feb 24 '11 at 11:34
  • 3
    @bestsss While some properties are only available to applets that are trusted: `java.specification.version`, `java.version` & `java.vm.version` are all available to a sand-boxed applet, or at least they were as of `1.6.0_23`. See [my answer](http://stackoverflow.com/a/5105344/418556) for more details. – Andrew Thompson Jun 13 '14 at 10:53
  • 1
    Also a thing that may work: `java -version` :) – badp Feb 23 '17 at 11:41
  • 2
    Possible duplicate of [Getting Java version at runtime](https://stackoverflow.com/questions/2591083/getting-java-version-at-runtime) – BuZZ-dEE Aug 15 '19 at 11:40

11 Answers11

153

System.getProperty("java.version") returns what you need.

You can also use JMX if you want:

ManagementFactory.getRuntimeMXBean().getVmVersion()

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • 6
    That JMX call returns the equivalent of "java.vm.version", not "java.version". These are usually (but not necessarily) the same. – Alex Miller Jun 21 '13 at 15:54
  • 2
    ManagementFactory.getRuntimeMXBean().getSpecVersion() may be more accurate. – auntyellow Dec 09 '14 at 06:04
  • When are they different, @AlexMiller? That sounds interesting. – DavidS Jul 08 '15 at 00:26
  • 2
    Actually, I'd say they're *not* the same. The java.vm.version is the jvm version number, something like "25.0-b70" whereas the java.version is the normal java language version you're used to seeing "1.8.0". – Alex Miller Jul 08 '15 at 02:19
  • 3
    Worth noting that the Java 9 will change the returned value from this string. – AlBlue Apr 20 '16 at 19:12
96

Use:

System.getProperty("java.version");

Where java.version can be replaced with one of the many other system properties related to the current Java version. Here is a table of them:

 Property                        Value (OpenJDK 12)                        Value (Oracle JRE 8u201)                Value (Sun JRE 5u22)                                 Description
------------------------------- ----------------------------------------- --------------------------------------- ---------------------------------------------------- ---------------------------------------------------------------------------------------------------------------
 java.version                    "12"                                      "1.8.0_201"                             "1.5.0_22"                                           Java Runtime Environment version, which may be interpreted as a Runtime.Version
 java.version.date               "2019-03-19"                              null                                    null                                                 Java Runtime Environment version date, in ISO-8601 YYYY-MM-DD format, which may be interpreted as a LocalDate
 java.vendor                     "Oracle Corporation"                      "Oracle Corporation"                    "Sun Microsystems Inc."                              Java Runtime Environment vendor
 java.vendor.version             null                                      null                                    null                                                 Java vendor version
 java.vendor.url                 "https://java.oracle.com/"                "http://java.oracle.com/"               "http://java.sun.com/"                               Java vendor URL
 java.vendor.url.bug             "https://bugreport.java.com/bugreport/"   "http://bugreport.sun.com/bugreport/"   "http://java.sun.com/cgi-bin/bugreport.cgi"          Undocumented
 java.specification.name         "Java Platform API Specification"         "Java Platform API Specification"       "Java Platform API Specification"                    Java Runtime Environment specification name
 java.specification.vendor       "Oracle Corporation"                      "Oracle Corporation"                    "Sun Microsystems Inc."                              Java Runtime Environment specification vendor
 java.specification.version      "12"                                      "1.8"                                   "1.5"                                                Java Runtime Environment specification version, whose value is the feature element of the runtime version
 java.vm.name                    "OpenJDK 64-Bit Server VM"                "Java HotSpot(TM) 64-Bit Server VM"     "Java HotSpot(TM) 64-Bit Server VM"                  Java Virtual Machine implementation name
 java.vm.vendor                  "Oracle Corporation"                      "Oracle Corporation"                    "Sun Microsystems Inc."                              Java Virtual Machine implementation vendor
 java.vm.version                 "12+33"                                   "25.201-b09"                            "1.5.0_22-b03"                                       Java Virtual Machine implementation version which may be interpreted as a Runtime.Version
 java.vm.info                    "mixed mode, sharing"                     "mixed mode"                            "mixed mode"                                         Undocumented
 java.vm.specification.name      "Java Virtual Machine Specification"      "Java Virtual Machine Specification"    "Java Virtual Machine Specification"                 Java Virtual Machine specification name
 java.vm.specification.vendor    "Oracle Corporation"                      "Oracle Corporation"                    "Sun Microsystems Inc."                              Java Virtual Machine specification vendor
 java.vm.specification.version   "12"                                      "1.8"                                   "1.0"                                                Java Virtual Machine specification version, whose value is the feature element of the runtime version
 java.runtime.name               "OpenJDK Runtime Environment"             "Java(TM) SE Runtime Environment"       "Java(TM) 2 Runtime Environment, Standard Edition"   Undocumented
 java.runtime.version            "12+33"                                   "1.8.0_201-b09"                         "1.5.0_22-b03"                                       Undocumented
 java.class.version              "56.0"                                    "52.0"                                  "49.0"                                               Java class format version number
 jdk.debug                       "release"                                 null                                    null                                                 Undocumented
 sun.java.launcher               "SUN_STANDARD"                            "SUN_STANDARD"                          "SUN_STANDARD"                                       Undocumented
 sun.management.compiler         "HotSpot 64-Bit Tiered Compilers"         "HotSpot 64-Bit Tiered Compilers"       "HotSpot 64-Bit Server Compiler"                     Undocumented

Sources:

  • Output of java -XshowSettings:all -version for a variety of JVM versions.
  • Java API Reference documentation for System.getProperties()
MultiplyByZer0
  • 6,302
  • 3
  • 32
  • 48
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
61

It seems the java.specification.version is the best one for the job.

E.G.

java.specification.version  1.6
java.version    1.6.0_23
java.vm.version 19.0-b09
java.runtime.version    1.6.0_23-b05
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 2
    `java.vm.version` is null in openjdk-11-headless – User8461 Mar 09 '19 at 20:06
  • 3
    @User8461 Maybe you should raise a bug report with whoever oversees the open JDK, To be honest I've not seen much point to finding out that value. Here it is currently `25.45-b02` .. which means nothing (provides no useful information) to me. – Andrew Thompson Mar 09 '19 at 20:14
13

Simply a case of calling System.getProperty("java.version").

Finbarr
  • 31,350
  • 13
  • 63
  • 94
5

Just simply call,

System.out.println(System.getProperty("java.specification.version"));
System.out.println(System.getProperty("java.runtime.version"));

Example output:

9
9+176
2

Below java code returns JVM versions which are available in your current IDE

List<VirtualMachineDescriptor> descriptors = VirtualMachine.list();
          for (VirtualMachineDescriptor descriptor : descriptors) {
              System.out.println("Found JVM: " + descriptor.displayName());
              try {
                  VirtualMachine vm = VirtualMachine.attach(descriptor);
                  String version = vm.getSystemProperties().getProperty("java.runtime.version");
                  System.out.println("   Runtime Version: " + version);

                   String connectorAddress = vm.getAgentProperties().getProperty("com.sun.management.jmxremote.localConnectorAddress");
                  if (connectorAddress == null) {

                      connectorAddress = vm.getAgentProperties().getProperty("com.sun.management.jmxremote.localConnectorAddress");
                  }

                  JMXServiceURL url = new JMXServiceURL(connectorAddress);
                  JMXConnector connector = JMXConnectorFactory.connect(url);
                  MBeanServerConnection mbs = connector.getMBeanServerConnection();

                  ObjectName threadName = new ObjectName(ManagementFactory.THREAD_MXBEAN_NAME);
                  Integer threadCount = (Integer)mbs.getAttribute(threadName, "ThreadCount");
                  System.out.println("    Thread count: " + threadCount);
              }
              catch (Exception e) {
                  // ...
              }

output:

Found JVM: /home/.../STS IDE/sts-bundle/sts-3.7.2.RELEASE//plugins/org.eclipse.equinox.launcher_1.3.100.v20150511-1540.jar -os linux -ws gtk -arch x86_64 -showsplash -launcher /home/.../STS IDE/sts-bundle/sts-3.7.2.RELEASE/STS -name STS --launcher.library /home/.../STS IDE/sts-bundle/sts-3.7.2.RELEASE//plugins/org.eclipse.equinox.launcher.gtk.linux.x86_64_1.1.300.v20150602-1417/eclipse_1612.so -startup /home/.../STS IDE/sts-bundle/sts-3.7.2.RELEASE//plugins/org.eclipse.equinox.launcher_1.3.100.v20150511-1540.jar --launcher.overrideVmargs -exitdata 1ad000f -product org.springsource.sts.ide -vm /usr/bin/java -vmargs -Dosgi.requiredJavaVersion=1.7 -Xms40m -XX:MaxPermSize=256m -Xverify:none -Xmx1200m -jar /home/.../STS IDE/sts-bundle/sts-3.7.2.RELEASE//plugins/org.eclipse.equinox.launcher_1.3.100.v20150511-1540.jar
   Runtime Version: 1.8.0_91-b14
Found JVM: com.intellij.idea.Main
   Runtime Version: 1.8.0_91-b14
Found JVM: Test
   Runtime Version: 1.7.0_80-b15
2

Depending on what one needs, the other answers can help.

In my case, they didn't. I was looking for the "fully qualified" version information of a IBM JDK.

So, the "real" answer can be: just dump all system properties and check if there is one that gives you what you are looking for.

In my case; I found that the IBM JDK knows a

Property: java.fullversion

JRE 1.8.0 IBM J9 2.8 Linux amd64-64 Compressed References 20161013_322271 (JIT enabled, AOT enabled)

J9VM - R28_Java8_SR3_20161013_1635_B322271

JIT - tr.r14.java.green_20161011_125790

GC - R28_Java8_SR3_20161013_1635_B322271_CMPRSS J9CL - 20161013_322271

Community
  • 1
  • 1
GhostCat
  • 137,827
  • 25
  • 176
  • 248
2

Since Java 9 we have a new static method: Runtime.version().

The object returned has interesting methods such as feature() or compareToIgnoreOptional() what might be easier to work with (e.g. Runtime.version().feature() >= 11).

Adam Kotwasinski
  • 4,377
  • 3
  • 17
  • 40
  • Unfortunately I couldn't find a nice list of constant fields, so we can't write `Runtime.version(). compareToIgnoreOptional(Something.V11)` or similar. A kinda-workaround would be to use `Runtime.Version.parse("11")`. – Adam Kotwasinski Dec 06 '21 at 21:14
0

Information about versions is stored as a properties of System class.

http://docs.oracle.com/javase/6/docs/api/java/lang/System.html#getProperties%28%29

Abbath
  • 51
  • 7
0

System.getProperty("sun.arch.data.model");

Java 32 Bit and 64 Bit Control

    Integer vers = Integer.parseInt(System.getProperty("java.version").split("\\.")[1]);
    String bitMode = System.getProperty("sun.arch.data.model").toString();
    System.out.println(vers);
    System.out.println(bitMode);

Output :

6
32
0

we can use the JVM runtime's class bytecode version to check against required JVM release:

This solution doesn't depend on any Enum constants etc and is backward compatible.

NOTE: This solution works irrespective of the bytecode version of the current executing class or other classes/jars in runtime classpath because the property reflects the assigned bytecode version for a given JDK release.

example: if a class is compiled at sourceCompatibility/targetCompatiblity 1.8/release level 8, but if it is executed using Java 11 JDK/runtime, then the java.class.version system property value will still be 55(Java11) though the executing class bytecode level is 52(Java8).

public class Java9Check{
    public static void main(String[] args){
        String bytecodeStr = System.getProperty("java.class.version");
        try {
            double bytecodeVal = Double.parseDouble(bytecodeStr);
            int bytecode = (int) bytecodeVal;
            // if Java 8 or below, return ExitCode(1)
            if (bytecode < 53) {
                System.out.println("Java8 or below");
                System.exit(1);
            }
            System.out.println("Java9 or above");
        } catch (NumberFormatException ignored) {}
        // proceed with normal exit, ExitCode(0)
    }
}
JDK Version  Class File Version
Java 1.0     45.0
Java 1.1     45.3
Java 1.2     46.0
Java 1.3     47.0
Java 1.4     48.0
Java 5       49.0
Java 6       50.0
Java 7       51.0
Java 8       52.0
Java 9       53.0
Java 10      54.0
Java 11      55.0
Java 12      56.0
Java 13      57.0
Java 14      58.0
Java 15      59.0
Java 16      60.0
Java 17      61.0
Java 18      62.0
Java 19      63.0
Java 20      64.0
Java 21      65.0
Java 22      66.0
mahee96
  • 705
  • 6
  • 15