How to find the JVM version from a program … with

2019-09-06 11:48发布

I have read this, but it ain't good enough!

What I mean is: when i run

java -version

I get:

java version "1.8.0"
Java(TM) SE Runtime Environment (build pxa6480sr3fp20-20161019_02(SR3 FP20))
IBM J9 VM (build 2.8, JRE 1.8.0 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

...

But when I use any of the programmatic ways to access the JVM version "from within" (as outlined by the question I am linking to), I only see values such as:

Property: java.vm.specification.version   : 1.8
Property: java.vm.specification.vendor    : Oracle Corporation
Property: java.vm.specification.name      : Java Virtual     Machine Specification
Property: java.vm.version                 : 2.8
Property: java.vm.vendor                  : IBM Corporation
Property: java.vm.name                    : IBM J9 VM
Property: java.specification.version      : 1.8
Property: java.specification.vendor       : Oracle Corporation
Property: java.specification.name         : Java Platform API Specification

Using

MX bean --- vmversion: 2.8

My problem: our build server runs a slightly older version of IBM java. java -version would show that, like

J9VM - R28_Java8_SR1_20150410_1531_B243669 

But: when I print those system properties, such details are lost! I have not found a way to write java code that would be able to distinguish that slightly older JVM from the newer one.

Long story short: is there any programmatic way to do something "within my JVM" that would give me that level of detail?

(basically I need that to dynamically disable some of our JUnit tests; they simply dont work with the older JVM; but I want them to be executed when running in my local eclipse installation with a newer JVM)

标签: java jvm version
1条回答
啃猪蹄的小仙女
2楼-- · 2019-09-06 12:26

I don't have IBM Java, only Oracle Java. When I do java -version I get:

java version "1.8.0_91"
Java(TM) SE Runtime Environment (build 1.8.0_91-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mode)

I can reproduce that exact output with this code:

System.out.printf("java version \"%s\"%n" +
                  "%s (build %s)%n" +
                  "%s (build %s, %s)%n",
                  System.getProperty("java.version"),
                  System.getProperty("java.runtime.name"),
                  System.getProperty("java.runtime.version"),
                  System.getProperty("java.vm.name"),
                  System.getProperty("java.vm.version"),
                  System.getProperty("java.vm.info"));

There might be other information added by IBM Java. Why not try dumping all system properties and see for yourself:

new TreeMap<>(System.getProperties()).entrySet().forEach(e ->
        System.out.printf("%s = %s%n", e.getKey(), e.getValue())
);
查看更多
登录 后发表回答