104

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

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?

Danubian Sailor
  • 1
  • 38
  • 145
  • 223
Daniel Stanca
  • 1,371
  • 4
  • 14
  • 16

10 Answers10

73

Of course you can use multiple versions of Java under Windows. 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 5 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 ...
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
reallyinsane
  • 1,192
  • 9
  • 8
  • 12
    On Windows, to stop the Java installer replacing your existing (first installed) Java install, you must pass it the STATIC=1 argument i.e. run from command line "jre-6u24-windows-i586-s.exe /STATIC=1" – Tom Chiverton Mar 16 '11 at 15:05
  • 4
    When I do "jdk-6u27-windows-x64.exe /STATIC=1" on Windows 7 64, it does not install, it just brings up a pop-up window showing the usage for msiexec. – yetimoner Oct 12 '11 at 00:01
  • 8
    According to [this document from Oracle](http://docs.oracle.com/javase/7/docs/webnotes/install/windows/jre-installer-options.html) it's actually `jdk-...exe STATIC=1`. I tried this and it worked. – Matt Aug 10 '13 at 03:37
  • 2
    Guys, you are confusing JDK and JRE. JRE does take `STATIC` as argument, but does JDK too? – Danijel Oct 16 '13 at 10:21
  • 1
    @reallyinsane I can't understand this. The answer is not simplified for a newbie. Can you link me to a complete tutorial? – abhimanyuaryan Jul 17 '15 at 23:01
  • 1
    @abhimanyuaryan many years later, but you would have learned more writing the tutorial yourself . For future readers, set the right environment variable(s) (as it said in the answer, `JAVA_HOME` and `PATH`) and your computer will know where to look for the correct "new" java. You also have to remove the old value(s) of `JAVA_HOME` and `PATH`. – Nathan majicvr.com May 07 '22 at 20:42
17

I was appalled at the clumsiness of the CLASSPATH, JAVA_HOME, and PATH ideas, in Windows, to keep track of Java files. I got here, because of multiple JREs, and how to content with it. Without regurgitating information, from a guy much more clever than me, I would rather point to to his article on this issue, which for me, resolves it perfectly.

Article by: Ted Neward: Multiple Java Homes: Giving Java Apps Their Own JRE

With the exponential growth of Java as a server-side development language has come an equivablent exponential growth in Java development tools, environments, frameworks, and extensions. Unfortunately, not all of these tools play nicely together under the same Java VM installation. Some require a Servlet 2.1-compliant environment, some require 2.2. Some only run under JDK 1.2 or above, some under JDK 1.1 (and no higher). Some require the "com.sun.swing" packages from pre-Swing 1.0 days, others require the "javax.swing" package names.

Worse yet, this problem can be found even within the corporate enterprise, as systems developed using Java from just six months ago may suddenly "not work" due to the installation of some Java Extension required by a new (seemingly unrelated) application release. This can complicate deployment of Java applications across the corporation, and lead customers to wonder precisely why, five years after the start of the infamous "Installing-this-app-breaks-my-system" woes began with Microsoft's DLL schemes, we still haven't progressed much beyond that. (In fact, the new .NET initiative actually seeks to solve the infamous "DLL-Hell" problem just described.)

This paper describes how to configure a Java installation such that a given application receives its own, private, JRE, allowing multiple Java environments to coexist without driving customers (or system administrators) insane...

gnat
  • 6,213
  • 108
  • 53
  • 73
Drikus
  • 171
  • 1
  • 2
  • 16
    I think the link given in above answer doesn't exist anymore.I found the same article here http://docplayer.net/15645854-Multiple-java-homes-giving-java-apps-their-own-jre-a-javageeks-com-white-paper.html – Alok Mishra Mar 04 '17 at 12:30
  • It's not a problem limited to Windows. But it is a problem that Microsoft solved 25 years ago. Why Java continues to use environment variables is beyond me. – Ian Boyd Jun 29 '22 at 18:13
14

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.

Moshe
  • 2,638
  • 2
  • 28
  • 32
  • 2
    I don't believe this is true for Windows. – cmcginty May 07 '13 at 23:47
  • @Casey, Which part are you referring to? Why is it not true? – Pacerier Aug 25 '14 at 06:24
  • 1
    [This answer](http://stackoverflow.com/a/5493139/3182836) explains the problem. The Windows registry is used to locate the default version of java. – Michelle Welcks Dec 10 '15 at 21:48
  • Although it is often true that a Java program will run fine on a new version of Java then the one it was developed with, there is no guarantee. Also, the JAVA_HOME environment variable needs to be updated in most cases. – Patrick O'Hara Feb 06 '18 at 21:23
6

I use a simple script when starting JMeter with my own java version

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

To have a java "portable" you can use this method here:

https://www.whitebyte.info/programming/java/how-to-install-a-portable-jdk-in-windows-without-admin-rights

Xbox One
  • 307
  • 2
  • 13
5

We can install multiple versions of Java Development kits on the same machine using SDKMan.

Some points about SDKMan are as following:

  1. SDKMan is free to use and it is developed by the open source community.
  2. SDKMan is written in bash and it only requires curl and zip/unzip programs to be present on your system.
  3. SDKMan can install around 29 Software Development Kits for the JVM such as Java, Groovy, Scala, Kotlin and Ceylon. Ant, Gradle, Grails, Maven, SBT, Spark, Spring Boot, Vert.x.
  4. We do not need to worry about setting the _HOME and PATH environment variables because SDKMan handles it automatically.

SDKMan can run on any UNIX based platforms such as Mac OSX, Linux, Cygwin, Solaris and FreeBSD and we can install it using following commands:

$ curl -s "https://get.sdkman.io" | bash  
$ source "$HOME/.sdkman/bin/sdkman-init.sh" 

Because SDKMan is written in bash and only requires curl and zip/unzip to be present on your system. You can install SDKMan on windows as well either by first installing Cygwin or Git Bash for Windows environment and then running above commands.

Command sdk list java will give us a list of java versions which we can install using SDKMan.

Installing Java 8

$ sdk install java 8.0.201-oracle

Installing Java 9

$ sdk install java 9.0.4-open 

Installing Java 11

$ sdk install java 11.0.2-open

Uninstalling a Java version

In case you want to uninstall any JDK version e.g., 11.0.2-open you can do that as follows:

$ sdk uninstall java 11.0.2-open

Switching current Java version

If you want to activate one version of JDK for all terminals and applications, you can use the command

sdk default java <your-java_version>

Above commands will also update the PATH and JAVA_HOME variables automatically. You can read more on my article How to Install Multiple Versions of Java on the Same Machine.

Naresh Joshi
  • 4,188
  • 35
  • 45
  • 5
    it's for non windows user. – gumuruh Oct 18 '20 at 08:40
  • @gumuruh You can install it windows as well, Because SDKMan is written in bash and only requires curl and zip/unzip to be present on your system. You can install SDKMan on windows as well by first installing Cygwin or Git Bash for Windows environment and then running above commands. – Naresh Joshi Oct 19 '20 at 15:29
4

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%.

Ruben
  • 9,056
  • 6
  • 34
  • 44
2

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
jan.supol
  • 2,636
  • 1
  • 26
  • 31
1

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jcoder
  • 29,554
  • 19
  • 87
  • 130
  • I believe this only works with "Public" JRE's. Excellent on client machines, a bit of a pain for developers who don't install the public JRE. Much better than a batch file. – James Schek Nov 07 '08 at 20:38
  • this option is no longer supported (at least as of v17; I get the error " Error: Specifying an alternate JDK/JRE version is no longer supported. The use of the flag '-version:' is no longer valid. Please download and execute the appropriate version. Unrecognized option: -version:8 Error: Could not create the Java Virtual Machine. Error: A fatal exception has occurred. Program will exit." – David LeBauer May 02 '22 at 17:39
0

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

1- 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>"

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