Getting Java version at runtime

I need to get around the Java bug in JDK 1.5, which was fixed in 1.6. I use the following condition:

if (System.getProperty("java.version").startsWith("1.5.")) { ... } else { ... } 

Will this work for other JVMs? Is there a better way to test this?

+104
java jvm
Apr 7 '10 at 8:56
source share
12 answers

These articles seem to suggest that checking the 1.5 or 1.6 prefix should work, as it matches the correct version naming convention.

Sun Technical Articles

+38
Apr 07 '10 at 9:08
source share

java.version is a standard property that exists in every JVM. There are two possible formats for this:

  • 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

Here is the trick to extract the main version: If it is 1.x.y_z version 1.x.y_z , extract the character at the index of 2 lines. If this is an xyz version string, trim the string to the first character of the dot, if one exists.

 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 conveniently:

 if(getVersion() < 6) { // ... } 
+61
Apr 07 2018-10-10T00:
source share

How to get version from meta infos package:

 String version = Runtime.class.getPackage().getImplementationVersion(); 

Prints something like:

1.7.0_13

+48
Jan 14 '14 at 11:34
source share

The easiest way ( 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 ):

 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 everything ( 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]; 
+27
May 13 '14 at 4:34
source share

Starting with Java 9, you can use Runtime.version() , which returns Runtime.Version :

 Runtime.Version version = Runtime.version(); 
+21
Jul 31 '17 at 13:57
source share

Doesn't work, you need --pos to evaluate double:

  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; } 
+9
Nov 15 '12 at 14:23
source share

Just note that in Java 9 and above, the naming convention is different. System.getProperty("java.version") returns "9" , not "1.9" .

+8
Sep 23 '17 at 9:18
source share

Example for 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 :) } 
+7
May 17 '14 at 1:59
source share

Here is the implementation in JOSM :

 /** * Returns the Java version as an int value. * @return the Java version as an int value (8, 9, etc.) * @since 12130 */ public static int getJavaVersion() { String version = System.getProperty("java.version"); if (version.startsWith("1.")) { version = version.substring(2); } // Allow these formats: // 1.8.0_72-ea // 9-ea // 9 // 9.0.1 int dotPos = version.indexOf('.'); int dashPos = version.indexOf('-'); return Integer.parseInt(version.substring(0, dotPos > -1 ? dotPos : dashPos > -1 ? dashPos : 1)); } 
+4
Mar 27 '18 at 12:05
source share

I donโ€™t know of any other way to verify this, but this: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html#getProperties () "implies that" java.version "is a standard system property, so I expect that he will work with other JVMs.

+2
Apr 07 '10 at 9:07
source share

If you have a dependency on apache utils, you can use org.apache.commons.lang3.SystemUtils.

  System.out.println("Is Java version at least 1.8: " + SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_1_8)); 
+2
Oct 23 '17 at 14:16
source share

Here is the answer from @mvanle converted to Scala: scala> val Array(javaVerPrefix, javaVerMajor, javaVerMinor, _, _) = System.getProperty("java.runtime.version").split("\\.|_|-b") javaVerPrefix: String = 1 javaVerMajor: String = 8 javaVerMinor: String = 0

+1
Nov 25 '17 at 18:06
source share



All Articles