How to know JDK version from within Java code

2019-02-06 05:36发布

How to know JDK version from within Java code

标签: version java
3条回答
Lonely孤独者°
2楼-- · 2019-02-06 06:09

Relying on the java.version string for anything else than showing to a human is fragile and will break if running on another Java implementation.

The only reliable way programatically is to use reflection to carefully ask if a given facility is available, and then select the appropriate code accordingly.

查看更多
爷、活的狠高调
3楼-- · 2019-02-06 06:23

I presume you mean just the Java version, in which case try this:

String version = System.getProperty("java.version");
查看更多
▲ chillily
4楼-- · 2019-02-06 06:26

If you need to know the bit version(32bit or 64 bit) as well, and you are running a hotSpot JVM, you can try this code:

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

No grantee that it will work for other Java implementaions rather than hotSpot.

查看更多
登录 后发表回答