我想编写一个Java示例文件,在其中我想知道类在哪个JVM版本中运行。有办法吗?
System.getProperty("java.version")
返回你需要的内容。
如果你想要的话,也可以使用 JMX:
ManagementFactory.getRuntimeMXBean().getVmVersion()
使用:
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
资料来源:
java -XshowSettings:all -version
输出。System.getProperties()
的说明看起来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
25.45-b02
,但对我来说并没有什么用处(没有提供有用的信息)。 - Andrew Thompson只需要调用System.getProperty("java.version")
方法即可。
只需要简单地调用
System.out.println(System.getProperty("java.specification.version"));
System.out.println(System.getProperty("java.runtime.version"));
示例输出:
9
9+176
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) {
// ...
}
输出:
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
根据需要,其他回答可能会有所帮助。
但在我的情况下并没有。我正在寻找IBM JDK的“完全限定”版本信息。
因此,真正的答案可以是:只需转储所有系统属性,并检查是否有一个属性提供了您要查找的信息。
对于我的情况,我发现IBM JDK知道一个名为:
属性:java.fullversion
JRE 1.8.0 IBM J9 2.8 Linux amd64-64 Compressed References 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
Runtime.version().feature() >= 11
)。Runtime.version().compareToIgnoreOptional(Something.V11)
或类似的代码。一种折中的方法是使用 Runtime.Version.parse("11")
。 - Adam KotwasinskiSystem.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
AccessController.doPrivileged(new PrivilegedAction<String>(...));
。 - bestsssjava.specification.version
、java.version
和java.vm.version
这三个属性都可以在沙盒小程序中使用,至少在1.6.0_23
版本中是可以的。请参考我的回答以获取更多细节信息。 - Andrew Thompsonjava -version
:) - badp