aspose file tools*
The moose likes Java in General and the fly likes Check Java version programmatically Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Check Java version programmatically" Watch "Check Java version programmatically" New topic
Author

Check Java version programmatically

Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Hi,

Is there any neat way to check that the version of Java Runtime Environment that you're running on is good enough for the program?

My idea is that I'd be able to build into the program somewhere the concept that it should run on, say, 1.6.0u1 or better, and the program should check whether it is running on such a JRE.

Of course, I know that System property "java.version" gives you the Java version. That makes it easy to test if you are running on a particular exact version of JRE. But what I am looking for is any API that can tell whether version X is as good or better than version Y.
[ October 17, 2007: Message edited by: Peter Chase ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I saw something once that used reflection to test for classes and methods that appeared in major and minor JVM releases. It started from 1.x and worked up until something was not found. Is that the kind of thing you're thinking of?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Well, I wasn't really wanting to get involved with how it worked. I hoped there was an API for it.

The thing that made me think there might be was the Apple API for choosing your Java, where you specify the version as something like "1.5.0+", meaning 1.5.0 or better.

Conveniently, though, I think I already have an implicit version check. My applications are launched via my own program, which uses JNI Invocation Interface. In this, you specify the JNI version you expect. Java 6 has a different JNI version to older Javas and I ask for the Java 6 version, so if I accidentally tried to start my program with a Java 5 or older, it wouldn't launch.
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
I thought a jvm would not run a jar file that was compiled on a version later than the jvm, I just was told recently that this is the case.


"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
That's somewhat true. In my case, the Windows stuff is compiled with -source 1.5 and -target 1.5, so it will run on a Mac with Java 5 (the Mac Java 6 is only just coming out now). But I want to make sure that, when it runs on Windows, it uses Java 6, for the improved performance and some important bug-fixes.
[ October 18, 2007: Message edited by: Peter Chase ]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Veering off topic perhaps ... I just watched an OSGi introductory video on InfoQ. An application "bundle" can contain multiple versions of jars, JNI modules, etc and declare which ones should be used based on JVM and OS. Pretty cool.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Check Java version programmatically
 
Similar Threads
java/sql/Savepoint Exception
JRE is installed or not?
Importing Certificate into JAVA Keystore
Importing Alternate Layout Managers
Parameter setting in JDK configuration