Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Check Java version programmatically

 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic