Adsense Ad

Tuesday, 29 September 2020

Run Multiple Java Versions On Windows

?Queries?
We have a couple of applications running on Java 8 and would like now to bring in an application based on Java 6. Can both java versions work together in Windows platform?

Is there any control panel to set the appropriate Java version for different applications, or any other way to set up, what version of Java will be used to run that particular application?

Solution
Of course we can use multiple versions of Java in Windows platform and different applications can use different Java versions. How is your application started? Usually you will have a batch file where there is something like

Java....

This will search the Java executable using the PATH variable. So if Java 8 is first on the PATH, you will have problems running a Java 6 application. You should then modify the batch file to use a certain Java version e.g. by defining a environment variable JAVA6HOME with the value C:\java\java6 (if Java 6 is installed in this directory) and change the batch file calling

%JAVA6HOME%\bin\java ...


It is absolutely possible to install side-by-side several JRE/JDK versions. Moreover, you don't have to do anything special for that to happen, as Sun is creating a different folder for each (under Program Files).

There is no control panel to check which JRE works for each application. Basically, the JRE that will work would be the first in your PATH environment variable. You can change that, or the JAVA_HOME variable, or create specific cmd/bat files to launch the applications you desire, each with a different JRE in path.


It should be possible changing setting the JAVA_HOME environment variable differently for specific applications.

When starting from the command line or from a batch script you can use 
set JAVA_HOME=C:\...\j2dskXXX to change the JAVA_HOME environment.

It is possible that you also need to change the PATH environment variable to use the correct java binary. To do this you can use 
set PATH=%JAVA_HOME%\bin;%PATH%.

Invoking Java with "java -version:1.5", etc. should run with the correct version of Java. (Obviously replace 1.5 with the version you want.) If Java is properly installed on Windows there are paths to the vm for each version stored in the registry which it uses so you don't need to mess about with environment versions on Windows.

Or use links. While it is rather unpleasant to update the PATH in a running environment, it's easy to recreate a link to a new version of JRE/JDK. So:

  • install different versions of JDK you want to use
  • create a link to that folder either by junction or by built-in mklink command
  • set the PATH to the link
  • If other version of java is to be used, delete the link, create a new one, PATH/JAVA_HOME/hardcoded scripts remain untouched
Also, use a simple script when starting jmeter with our own java version

setlocal 
set JAVA_HOME="c:\java8" 
set PATH=%JAVA_HOME%\bin;%PATH%; 
java -version

If you use Java Web Start (you can start applications from any URL, even the local file system) it will take care of finding the right version for your application.



Using Java Web Start, you can install multiple JRE, then call what you need. On win, you can make a .bat file:


  • Online Version:
<your_JRE_version\bin\javaws.exe> -localfile -J-Djnlp.application.href=<the url of .jnlp file.jnlp> -localfile -J "<path_temp_jnlp_file_.jnlp>"

  • Launch from cache:
<your_JRE_version\bin\javaws.exe> -localfile -J "<path_of_your_local_jnlp_file.jnlp>"