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

时间:2011-02-24 10:06:18

标签: java jvm version

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

10 个答案:

答案 0 :(得分:110)

System.getProperty("java.version")会返回您需要的内容。

如果您愿意,也可以使用JMX:

ManagementFactory.getRuntimeMXBean().getVmVersion()

答案 1 :(得分:48)

似乎java.specification.version是最适合这项工作的人。

E.G。来自我之前的一个网站上的小程序:

java.specification.version  1.6
java.version    1.6.0_23
java.vm.version 19.0-b09
java.runtime.version    unknown

unknown的{​​{1}}值仅表示applet不受信任。这是受信任的版本:

java.runtime.version

答案 2 :(得分:36)

使用:

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

来源:

答案 3 :(得分:12)

只需调用System.getProperty("java.version")

答案 4 :(得分:1)

使用java.versionjava.specification.versionjava.runtime.version的示例:Getting version of java in runtime

答案 5 :(得分:1)

java代码下面返回当前IDE中可用的CIImage版本

JVM

输出:

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) {
                  // ...
              }

答案 6 :(得分:1)

根据需要,其他答案可以提供帮助。

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

所以,&#34;真实&#34;答案可以是:只需转储所有系统属性,并检查是否有一个可以为您提供所需信息。

在我的情况下;我发现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

答案 7 :(得分:0)

答案 8 :(得分: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

答案 9 :(得分:0)

只需致电,

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

示例输出:

9
9+176