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

Detect JRE Version

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Ok, this is more of a curiosity question. But it may be interesting.
I was wondering if there is any way to detect the JRE version running on a PC using JAVA.
Here is the reason:
Suppose you could write an application and you used some API from 1.4 but the user is running 1.3 and they don't want to upgrade. Could you then say something like:
if (using JRE1.4)
{
//Perform 1.4 Specific API Code
}else
{
//Perform 1.3 Specific API Code
}
I hope this makes sense.
Thanks.


GenRocket - Experts at Building Test Data
Junaid Bhatra
Ranch Hand

Joined: Jun 27, 2000
Posts: 213
You could try using:

However be careful when you use classes from 1.4 & the target JRE is not 1.4. The problem is that some JRE's may implement eager loading of classes, i.e when your class is loaded, the JVM might try to resolve all symbolic references and load those classes as well up front. So you may end up getting ClassDefNotFoundErrors, even though you are detecting & avoiding the usage of 1.4 classes.
You can work-around this issue by using Reflection and avoiding any symbolic references to 1.4 classes in your code.
[ November 07, 2002: Message edited by: Junaid Bhatra ]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Be ware, though, that (I'm pretty darn sure) there is no standard or requirement of the JLS or JVMS as to the actual String returned when requesting a System property. Hopefully the returned String would always contain 1.4 or 1.3 and you could just search the whole String for such a substring - but again, there is no official standard or guarantee as to the contents of the String.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Guennadiy VANIN
Ranch Hand

Joined: Aug 30, 2001
Posts: 898
If to rephrase this problem, then does it sound like:
whether earlier version JRE/JVM can run Java application compiled with later JDK, i.e. with the use of the classes from later JDK?
It was what I tried to clear for me in various topics.
Let me guess...
"Write once, run anywhere" ("WORA") combined with practical impossibility to configure (remote) clients, and the general-sense security considerations logically dictate that Java application should be independent on JVM/JRE version.
In other words, JVM should be capable to load "later versions" classes from application (class or .jar).
The application, obviously, should be filled during compilation with content of developer's classes, and contain everything to be loaded by thin JVM/JRE, independently of which JDK version is installed locally, sorry remotely from developer, except dynamic references.
This my,is obvious to me, logic.
But does it correspond to reality?
?Certainly, I followed advise of Cindy and looked to extreme of my patience thru JVM spec, but I did not understand from where JVM/JRE do resolve references, especially on standard packages.
From client's JDK, from client's JRE, from jars, as it should have been?
It seems to me, it is not specified exactly (Well, by pure logic of "WORA", everything should be really included to compiled file).
Does JRE installation contain all JAVA's standard API classes?
?Then if JVM/JRE loads only what is needed, it is better/smaller than always bundle (excessive volumes) of/with JRE to clients.
[ November 22, 2002: Message edited by: G Vanin ]
 
Don't get me started about those stupid light bulbs.
 
subject: Detect JRE Version