java.version
is a standard property which exists in every VM.
There is just a tiny trick which might make your life easier: Search for the second dot and cut the string there. Then convert it to double
. Now, you can check the version much more comfortably:
if (version >= 1.5) ...
You can put this into static code of a class so it runs only once:
public static double JAVA_VERSION = getVersion ();
static double getVersion () {
String version = System.getProperty("java.version");
int pos = 0, count = 0;
for ( ; pos<version.length() && count < 2; pos ++) {
if (version.charAt(pos) == '.') count ++;
}
return Double.parseDouble (version.substring (0, pos));
}