File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Handle Java JRE version issues Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Handle Java JRE version issues" Watch "Handle Java JRE version issues" New topic

Handle Java JRE version issues

Sébastien Roux

Joined: Jan 22, 2007
Posts: 5
Dear guruz,

I'm developing a small utility lately that can be both executed in cmd line environment or as a GUI if no args are passed.
Now I'd like to test if the user as the right JRE before running everything in order to display an error message!
I found some code to get the JRE version but is there a better way of doing it (if version higher than 1.6 than OK else error message).

Thanks for any advice or link of interest.


Ankit Garg

Joined: Aug 03, 2008
Posts: 9462

Hi S�bastien! Welcome to javaranch.

S�bastien Roux wrote:I found some code to get the JRE version but is there a better way of doing it

You didn't give the code that you found so how can we tell if there is a better way of doing the thing . So please post the code that you found...

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Sébastien Roux

Joined: Jan 22, 2007
Posts: 5
This is what I have...

So the idea would be to test the version that is returned...
Quite basic indeed would it be something more professional/complete/reliable in order to handle warnings for every JRE lower than 1.6?

Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

That will work. But there are a couple of things to keep in mind.

1) A compiled class will not run under an older JVM, even if that class does not use anything specific to the later JVM. Take for example this simple class:

All the classes and methods it uses have been around since the early days of Java. If I compile it via Java 6:

It runs fine if I use a Java 1.6 JVM:

>\java\1.6\bin\java Hello
Hello from Java v1.6.0_11

But if I try to run it under Java 1.5, it doesn't run:

>\java\1.5\bin\java Hello
Exception in thread "main" java.lang.UnsupportedClassVersionError: Bad version number in .class file
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(
at Method)
at java.lang.ClassLoader.loadClass(
at sun.misc.Launcher$AppClassLoader.loadClass(
at java.lang.ClassLoader.loadClass(
at java.lang.ClassLoader.loadClassInternal(

That "Bad version number" means that I am trying to run newer code with an older JVM.
To solve this, I can compile the code using the source and target switch on the compiler:

>\java\1.6\bin\javac -source 1.2 -target 1.2

Now it will run fine under an older JVM:

>P:\java\1.4\bin\java Hello
Hello from Java v1.4.2_18

If any subsequent code uses a Java 6 feature, an Exception would get thrown. For example, here I use the Console class and System.getConsole() method added in Java 6:

>\java\1.6\bin\javac -source 1.2 -target 1.2

>\java\1.4\bin\java Hello
Hello from Java v1.4.2_18
Your code would stop executing here if the Java version was not correct
Exception in thread "main" java.lang.NoSuchMethodError: java.lang.System.console()Ljava/io/Console;
at javaranch.versionCheck.Hello.main(

One thing you can do which might make life easier is to write a Version checking class that after verifying the version calls a starter method in another class. Then you only need to worry about setting the source and target when you compile this version check class.

2) The second thing to keep in mind that this will return a string like "1.6.0_11". So you'll need to break up the string so that you can get the major and minor parts and compare them. (And if the micro and patch levels are important, you'll need to do that as well.) You can use String manipulation to do this such as indexOf(".") and indexOf("_"). Or you can use a regular expression to break it up:

Hope that helps.
Sébastien Roux

Joined: Jan 22, 2007
Posts: 5
MANY THANKS Mark for such a complete and instructive reply!
I really did not know about the 1st point indeed.
Regarding the second point I'll make pleasure parsing version string with some regex! By the way my favorite regex class is the Jakarta ORO one (Perl5Util)!

Thanks for your answer again, a real guru indeed!

Bert Bates

Joined: Oct 14, 2002
Posts: 8898
intermediate level

Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
I agree. Here's the link:
subject: Handle Java JRE version issues
It's not a secret anymore!