If my machine have multiple version of jre installed say jre 5, jre 6 and jre 7. Is it possible to force my application to use specific jre (say jre 5) at runtime?
-
1You can check which JRE was used when starting a program and fail with descriptive message if it doesn't meet your requirements.Tomasz Nurkiewicz– Tomasz Nurkiewicz2012-08-31 17:04:44 +00:00Commented Aug 31, 2012 at 17:04
-
i was wondering when we are able to see list of jre from java control panel(Windows and Mac OSX not sure about Linux), So we may able able to select what version we want to use.twid– twid2012-08-31 17:08:50 +00:00Commented Aug 31, 2012 at 17:08
7 Answers
Launch the app. using Java Web Start. It provides many options for Runtime Versioning.

All the ticked JREs are available for use by JWS launched apps.
Here is how you might select any 1.5 variant in the launch file.
<j2se version="1.5" />
Comments
I have on my computer 3 JDKs. Every JDK has own environment variable
%JAVA_HOME_1_5%
%JAVA_HOME_1_6%
%JAVA_HOME_1_7%
when I want use specific JDK, I set %JAVA_HOME% to this variable.
Also I use scripts for using different JDK. For example, if I want start Jboss using JDK 6, I run next script
set JAVA_HOME=%JAVA_HOME_1_6%;
%JBOSS_HOME%/run.bat
If I want execute runnable jar (using JDK 5) I run next script
%JAVA_HOME_1_5%/bin java MyJar.jar
If I want run maven with JDK 7, I am using next script
set JAVA_HOME=%JAVA_HOME_1_7%
mvn clean install -Dmaven.test.skip=true
etc.
So you can use any JDK using env. variables and simple scripts
Comments
In your running application use
String javaVersion = System.getProperty("java.specification.version");
To check which version of Java is being used to run your app. You can throw an exception if it doesn't match what you want.
1 Comment
java.specification.version (here reports '1.7').I found way to do it. We can allow application to select jre by javaw -version:"version String" as given in LINK
For Example : javaw -version:"1.6.*"
Comments
You can force the application by starting it using an absolute path on disk to a specific Java version.
1 Comment
You generally have to do this before you launch java. Inside a script you can look for the executable of a specific version of java and fail if that jre doesn't exist/isn't in a known location. Then after finding that version of java run your java program with that jre. How to explicitly do this depends on your OS.
Once inside the java program java you can't change versions. Hence java checks for version (inside of a java program) are only useful for quitting before full executing rather than changing to a valid version.