如何从 Java 代码中知道 JDK 版本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4638994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to know JDK version from within Java code
提问by Muhammad Hewedy
How to know JDK version from within Java code
如何从 Java 代码中知道 JDK 版本
回答by Bart Kiers
I presume you mean just the Java version, in which case try this:
我想你的意思只是 Java 版本,在这种情况下试试这个:
String version = System.getProperty("java.version");
回答by Thorbj?rn Ravn Andersen
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.
依赖 java.version 字符串来做任何事情而不是向人类展示是脆弱的,如果在另一个 Java 实现上运行就会中断。
The onlyreliable way programatically is to use reflection to carefully ask if a given facility is available, and then select the appropriate code accordingly.
的唯一可靠的方法是编程使用反射来仔细问,如果给定的设施是可用的,然后据此选择合适的代码。
回答by ZhaoGang
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:
如果您还需要知道位版本(32 位或 64 位),并且您正在运行热点 JVM,您可以尝试以下代码:
System.getProperty("sun.arch.data.model")
System.getProperty("sun.arch.data.model")
No grantee that it will work for other Java implementaions rather than hotSpot.
没有受让人认为它适用于其他 Java 实现而不是热点。