1

I need to validate java version. I use

String version = System.getProperty("java.version");

How to simple parse that to know for example that installed JRE is in min. 1.6.0_18 version ? I wonder is that naming convention of java version is standard.

Nishant
  • 54,584
  • 13
  • 112
  • 127
marioosh
  • 27,328
  • 49
  • 143
  • 192
  • 4
    This has already been answered: http://stackoverflow.com/questions/2591083/getting-version-of-java-in-runtime – Endy Aug 28 '12 at 07:25
  • That answer is about check main version 1.5 vs 1.6. I need to validate more (`.0_18`, `.0-beta1` etc) – marioosh Aug 28 '12 at 07:54
  • @marioosh i guess my answer will do what you want, just leave a comment if unclear or something – nurgan Aug 28 '12 at 08:02

2 Answers2

2

Yes, there is naming convention. You can find it from here. And more fresh information about version 6 can be found from here.

Mikko Maunu
  • 41,366
  • 10
  • 132
  • 135
0

yes, this is as far as i know the naming convention of java Versions, so just use the System.getProperty("java.version") as you are already doing, and check the String by using stringTokenizer if the minimum required jre is installed.

Edit: i wrote Stringbuilder in the beginning, of cource i meant stringtokenizer.

With the Stringtokenizer you can get Substrings of a String separated by a defined Symbol, and kind of iterate through the String. You may need a second stringtokenizer, the first separating the Strting by "." and than for the last Substring one separating by "_". Take a lool here.

You could do it like this: (of cource you could also do it over a loop, an checking with hasMoreTokens() and so on, but you dont need in this case, because you know exactly what the string looks like)

String[] versionArray = new String[4];
String version = System.getProperty("java.version"); 
StringTokenizer st1 = new StringTokenizer(version,".");
versionArray[1] = st1.nextToken();
versionArray[2] = st1.nextToken();
StringTokenizer st2 = new StringTokenizer(st1.nextToken(),"_");
versionArray[3] = st2.nextToken();
versionArray[4] = st2.nextToken();

After it in the versionArray would be with your example ["1"]["6"]["0"]["18"] and you can do your comparison on this...

nurgan
  • 309
  • 1
  • 4
  • 22
  • Do u mean System.getPerperty("java.version").Please make it correct. – JDGuide Aug 28 '12 at 07:30
  • @JDeveloper uhm, no i mean `public static String getProperty(String key)` as specified [here](http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/System.html) – nurgan Aug 28 '12 at 07:41