在运行时获取 Java 版本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2591083/
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
Getting Java version at runtime
提问by list
I need to work around a Java bug in JDK 1.5 which was fixed in 1.6. I'm using the following condition:
我需要解决 JDK 1.5 中的 Java 错误,该错误已在 1.6 中修复。我正在使用以下条件:
if (System.getProperty("java.version").startsWith("1.5.")) {
...
} else {
...
}
Will this work for other JVMs? Is there a better way to check this?
这是否适用于其他 JVM?有没有更好的方法来检查这个?
采纳答案by polygenelubricants
These articles seem to suggest that checking for 1.5
or 1.6
prefix should work, as it follows proper version naming convention.
这些文章似乎建议检查1.5
或1.6
前缀应该起作用,因为它遵循正确的版本命名约定。
Sun Technical Articles
Sun 技术文章
- J2SE SDK/JRE Version String Naming Convention
- Version 1.5.0 or 5.0?
- "J2SE also keeps the version number 1.5.0 (or 1.5) in some places that are visible only to developers, or where the version number is parsed by programs"
- "
java.version
system property"
- "
- "J2SE also keeps the version number 1.5.0 (or 1.5) in some places that are visible only to developers, or where the version number is parsed by programs"
- Version 1.6.0 Used by Developers
- "Java SE keeps the version number 1.6.0 (or 1.6) in some places that are visible only to developers, or where the version number is parsed by programs."
- "
java.version
system property"
- "
- "Java SE keeps the version number 1.6.0 (or 1.6) in some places that are visible only to developers, or where the version number is parsed by programs."
- J2SE SDK/JRE 版本字符串命名约定
- 版本 1.5.0 还是 5.0?
- “J2SE 还在某些仅对开发人员可见的地方,或在程序解析版本号的地方保留了版本号 1.5.0(或 1.5)”
- “
java.version
系统属性”
- “
- “J2SE 还在某些仅对开发人员可见的地方,或在程序解析版本号的地方保留了版本号 1.5.0(或 1.5)”
- 开发人员使用的 1.6.0 版本
- “Java SE 将版本号 1.6.0(或 1.6)保留在一些只有开发人员可见的地方,或者版本号由程序解析的地方。”
- “
java.version
系统属性”
- “
- “Java SE 将版本号 1.6.0(或 1.6)保留在一些只有开发人员可见的地方,或者版本号由程序解析的地方。”
回答by Aaron Digulla
java.version
is a system propertythat exists in every JVM. There are two possible formats for it:
java.version
是存在于每个 JVM 中的系统属性。它有两种可能的格式:
- Java 8 or lower:
1.6.0_23
,1.7.0
,1.7.0_80
,1.8.0_211
- Java 9 or higher:
9.0.1
,11.0.4
,12
,12.0.1
- Java 8 或更低版本:
1.6.0_23
,1.7.0
,1.7.0_80
,1.8.0_211
- Java 9 或更高版本:
9.0.1
,11.0.4
,12
,12.0.1
Here is a trick to extract the major version: If it is a 1.x.y_z
version string, extract the character at index 2 of the string. If it is a x.y.z
version string, cut the string to its first dot character, if one exists.
这里有一个提取主要版本的技巧:如果它是一个1.x.y_z
版本字符串,则提取字符串索引 2 处的字符。如果它是一个x.y.z
版本字符串,则将该字符串剪切到它的第一个点字符(如果存在)。
private static int getVersion() {
String version = System.getProperty("java.version");
if(version.startsWith("1.")) {
version = version.substring(2, 3);
} else {
int dot = version.indexOf(".");
if(dot != -1) { version = version.substring(0, dot); }
} return Integer.parseInt(version);
}
Now you can check the version much more comfortably:
现在您可以更轻松地检查版本:
if(getVersion() < 6) {
// ...
}
回答by Tom Jefferys
Don't know another way of checking this, but this: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html#getProperties()" implies "java.version" is a standard system property so I'd expect it to work with other JVMs.
不知道检查这个的另一种方法,但这个: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html#getProperties()”暗示“java.version”是一个标准的系统属性,所以我希望它与其他 JVM 一起工作。
回答by Alessandro
Does not work, need --pos
to evaluate double:
行不通,需要--pos
双重评估:
String version = System.getProperty("java.version");
System.out.println("version:" + version);
int pos = 0, count = 0;
for (; pos < version.length() && count < 2; pos++) {
if (version.charAt(pos) == '.') {
count++;
}
}
--pos; //EVALUATE double
double dversion = Double.parseDouble(version.substring(0, pos));
System.out.println("dversion:" + dversion);
return dversion;
}
回答by Stefan
What about getting the version from the package meta infos:
从包元信息中获取版本怎么样:
String version = Runtime.class.getPackage().getImplementationVersion();
Prints out something like:
打印出如下内容:
1.7.0_13
1.7.0_13
回答by mvanle
The simplest way (java.specification.version):
最简单的方法(java.specification.version):
double version = Double.parseDouble(System.getProperty("java.specification.version"));
if (version == 1.5) {
// 1.5 specific code
} else {
// ...
}
or something like (java.version):
或类似(java.version):
String[] javaVersionElements = System.getProperty("java.version").split("\.");
int major = Integer.parseInt(javaVersionElements[1]);
if (major == 5) {
// 1.5 specific code
} else {
// ...
}
or if you want to break it all up (java.runtime.version):
或者如果你想把它全部分解(java.runtime.version):
String discard, major, minor, update, build;
String[] javaVersionElements = System.getProperty("java.runtime.version").split("\.|_|-b");
discard = javaVersionElements[0];
major = javaVersionElements[1];
minor = javaVersionElements[2];
update = javaVersionElements[3];
build = javaVersionElements[4];
回答by mvanle
Example for Apache Commons Lang:
Apache Commons Lang 示例:
import org.apache.commons.lang.SystemUtils;
Float version = SystemUtils.JAVA_VERSION_FLOAT;
if (version < 1.4f) {
// legacy
} else if (SystemUtils.IS_JAVA_1_5) {
// 1.5 specific code
} else if (SystemUtils.isJavaVersionAtLeast(1.6f)) {
// 1.6 compatible code
} else {
// dodgy clause to catch 1.4 :)
}
回答by Eng.Fouad
Since Java 9, you can use Runtime.version()
, which returns a Runtime.Version
:
从 Java 9 开始,您可以使用Runtime.version()
,它返回一个Runtime.Version
:
Runtime.Version version = Runtime.version();
回答by Sina Madani
Just a note that in Java 9 and above, the naming convention is different. System.getProperty("java.version")
returns "9"
rather than "1.9"
.
请注意,在 Java 9 及更高版本中,命名约定是不同的。System.getProperty("java.version")
返回"9"
而不是"1.9"
.
回答by P.Kurek
If you can have dependency to apache utils you can use org.apache.commons.lang3.SystemUtils.
如果您可以依赖 apache utils,您可以使用 org.apache.commons.lang3.SystemUtils。
System.out.println("Is Java version at least 1.8: " + SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_1_8));