3

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?

2
  • 1
    You can check which JRE was used when starting a program and fail with descriptive message if it doesn't meet your requirements. Commented 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. Commented Aug 31, 2012 at 17:08

7 Answers 7

2

Launch the app. using Java Web Start. It provides many options for Runtime Versioning.

JNLP Runtime Settings

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" />
Sign up to request clarification or add additional context in comments.

Comments

2

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

1

Run the java in corresponding paths:

usr/local/jdk1.5/bin/java HelloWorld

usr/local/jdk1.6/bin/java HelloWorld

usr/local/jdk1.7/bin/java HelloWorld

etc...

Comments

1

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

"You can parse out the major version" Easier to use the java.specification.version (here reports '1.7').
1

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

0

You can force the application by starting it using an absolute path on disk to a specific Java version.

1 Comment

that wont be possible because i need to select jre before actual application starts.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.