如何从程序中找到JVM版本?


116

我想编写一个示例Java文件,在其中我想知道运行该类的JVM版本。有办法吗?


3
一些笔记。系统属性仅用于此目的,但请记住,这是一项特权操作,并且applets / webstart / sand装箱的代码将无法执行它(获取SecurityException)。通常你想以同样的方式运行AccessController.doPrivileged(new PrivilegedAction<String>(...));
bestsss

3
@bestsss虽然某些属性仅可用于受信任的applet :java.specification.versionjava.versionjava.vm.version都可用于沙盒applet,或者至少它们在时有效1.6.0_23。请参阅我的答案以获取更多详细信息。
Andrew Thompson

也可能java -version
有用

Answers:


127

System.getProperty("java.version") 返回您所需要的。

您还可以根据需要使用JMX:

ManagementFactory.getRuntimeMXBean().getVmVersion()


6
该JMX调用返回的等效项是“ java.vm.version”,而不是“ java.version”。这些通常(但不一定)相同。
Alex Miller

2
ManagementFactory.getRuntimeMXBean()。getSpecVersion()可能更准确。
auntyellow 2014年

他们什么时候不同,@ AlexMiller?听起来很有趣。
DavidS,2015年

1
其实我会说他们一样。java.vm.version是jvm版本号,类似“ 25.0-b70”,而java.version是您习惯于看到“ 1.8.0”的普通Java语言版本。
亚历克斯·米勒

3
值得一提的是,Java 9将更改此字符串的返回值。
AlBlue

54

看来,这java.specification.version是工作的最佳选择。

例如

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

1
java.vm.version在openjdk-11-headless中为null
User8461

@ User8461也许您应该向监督开放JDK的任何人提出一个错误报告,说实话,我发现这个价值并没有多大意义。目前在这里25.45-b02..对我来说什么都没有(没有提供有用的信息)。
Andrew Thompson

53

用:

System.getProperty("java.version");

在哪里java.version可以与当前的Java版本的许多其他系统属性中的一种替代。这是它们的表:

 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

资料来源:


12

只是一个呼叫的情况System.getProperty("java.version")



1

下面的Java代码返回JVM当前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) {
                  // ...
              }

输出:

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

1

根据一个人的需要,其他答案可以有所帮助。

就我而言,他们没有。我正在寻找IBM JDK的“完全合格”版本信息。

因此,“真实的”答案可能是:只需转储所有系统属性,然后检查是否有一个可以满足您的需求。

就我而言 我发现IBM JDK知道

属性:java.fullversion

JRE 1.8.0 IBM J9 2.8 Linux amd64-64压缩参考20161013_322271(已启用JIT,已启用AOT)

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


1

只是简单地打电话,

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

输出示例:

9
9+176


0

System.getProperty(“ sun.arch.data.model”);

Java 32位和64位控件

    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);

输出:

6
32
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.