As a Java programmer, I work on various projects based on different Java versions, especially since the transition to the six-month release cycle. From time to time, I also have a piece of Java code that I want to try out on different Java versions – on the command line without having to click through the menus of my IDE. So it is helpful to be able to quickly and easily change the Java version to be used for compiling code or executing class files.
In this article, I'll show you how to install several Java versions simultaneously in Windows and how to switch between them on the command line with short commands.
Installing several Java versions at the same time is incredibly easy in Windows. You can download and run the installer for each version, which automatically installs the versions in separate directories.
C:\jdk1.2.2\
and C:\Program Files (x86)\JavaSoft\JRE\1.2\
by default – I recommend changing this to C:\Program Files (x86)\Java\jdk1.2.2\
and C:\Program Files (x86)\Java\jre1.2.2\
for the sake of clarity.C:\jdk1.3.1_28\
by default – I recommend changing this to C:\Program Files (x86)\Java\jdk1.3.1_28\
. C:\j2sdk1.4.2_19\
by default – I recommend changing this to C:\Program Files (x86)\Java\jdk1.4.2_19\
.Starting with the following versions, you don't need to change the default installation directories:
Attention – you may use the following Oracle distributions only for private purposes and development:
The following version is currently an early access build. You should use it only for testing purposes:
In most cases, the following two environment variables decide which Java version an application uses:
JAVA_HOME
– many start scripts use this variable.Path
– is used when running a Java binary (such as java
and javac
) from the console.These variables should always point to the same Java installation to avoid unforeseen problems due to inconsistencies. Some programs, such as Eclipse, define the Java version in a separate configuration file (for Eclipse, for example, this is the entry "-vm" in the eclipse.ini
file).
The installers of the Java versions listed above already create various environment variables, which you need to clean up first (see below). The fastest way to change the environment variables is to press the Windows key and type "env" – Windows then offers "Edit the system environment variables" as a search result:
At this point, you can press "Enter," and the system properties appear:
Here you click on "Environment Variables…", after which the following window opens:
As the standard version, I currently use the latest release version, Java 15. Therefore, you should make the following settings:
C:\ProgramData\Oracle\Java\javapath
C:\Program Files (x86)\Common Files\Oracle\Java\javapath
%JAVA_HOME%\bin
The path list should then look like this:
The last entry ensures that Path
and JAVA_HOME
are automatically consistent. Attention: this only works for the default setting configured here. If you change JAVA_HOME
via the command line, you have to adjust Path
accordingly (more about this below).
Now open a command line to check the settings with the following commands:
echo %JAVA_HOME%
java -version
As a result, you should see this:
Finally, we come to the possibility to change the Java version quickly and easily. Therefore I create a separate command for each Java version, called, for example, java15
, java14
, java13
, java12
. I put these commands in the directory C:\Program Files\Java\scripts
, which I add to the environment variable "Path" as described before:
The files are named, for example, java15.bat
, java14.bat
, java13.bat
, java12.bat
, and change the environment variables as shown in the following example of java15.bat
:
@echo off
set JAVA_HOME=C:\Program Files\Java\jdk-15
set Path=%JAVA_HOME%\bin;%Path%
echo Java 15 activated.
This script, admittedly kept very simple, inserts the bin
directory of the Java version to be activated at the beginning of the Path
variable, so that is where, for example, java.exe
is looked for first. The path variable becomes longer with each switching. However, since this is only the case for the currently opened command prompt, I don't think this is a problem.
The following RAR file contains all my scripts from Java 1.2 to Java 16 (you might have to adopt one or the other Java directory to your installation): scripts-up-to-jdk16.zip
You can quickly test whether the scripts work in a command line. Attention: after adding the scripts
directory to the Path
variable, you have to open a new command line.
At this point, the setup is complete. I hope this article was helpful for you, and I'd love to hear your comments. If you liked the article, I'd be happy if you shared it via one of the following share icons.
I did this and now I can use command prompts to switch between versions as verified with "java -version" BUT I still cannot get my browser based interface to function correctly on the version 6. It DOES function correctly if I uninstall all Javas and reinstall just 6. Does this method not work for browser based applications?
Java folder is not editable. we cannot create any file in it.
Just wanted to leave a side note, I had found that whilst I had JAVA_HOME set to jdk 1.6 and had removed from PATH:
- C:\ProgramData\Oracle\Java\javapath
- C:\Program Files (x86)\Common Files\Oracle\Java\javapath
I had to move the %JAVA_HOME%\bin to the first option in the PATH as before when it was last in the list of values, when I ran java -version in cmd, it reported java 1.8 instead of java 1.6 which did show when I ran: echo %JAVA_HOME%
Might be something to do with the PATH value %SystemRoot%\system32 which is high up in the list and appears to have a java.exe placed within (might have been placed by the jdk8 installation perhaps?)
Finally solution that WORKS. Thats also so great that you provided scripts.
You just need to remember that after switching java version with script and closing terminal, next terminal will open with java default version, not the one from last script run.
Thank you!
Really Appreciated for this post
But it is only working in the command prompt.
I am using gitbash here it is not working.
can you assist where I can change it?
Once I switch the version how would I keep it same across?
Sven, there is a side affect in this script. Every time you run one of those scripts, a new JAVA_HOME entry is prepended to your path. As an example, I ran java8.bat three times in a row, and this is the start of my PATH: "C:\Program Files\Java\jre1.8.0_201\bin;C:\Program Files\Java\jre1.8.0_201\bin;C:\Program Files\Java\jre1.8.0_201\bin".
Hi Kolton, yes I know, I couldn't find an easy way to fix this. But the changes are not permanent, they only apply to the current prompt. Therefore I think it is OK.
Thank you! This helps a lot! especially the script!
Just a little correction:
If you add JAVA_HOME = C:\Program Files\Java\jdk-15\ and then add "%JAVA_HOME%\bin" to Path, the command "echo %Path%" displays: "C:\Program Files\Java\jdk-15\\bin" (with double \\). Therefore, I had to remove the last \ from JAVA_HOME.
Forgot to mention that the article was very helpful. Thank you very much!
Thanks for the correction! You are absolutely right. I've changed it in the screenshots and text.
Hello everyone.
First of all thanks for the script.
In my case the change of jdk worked but each time a new terminal was launched the default jdk came back ...
As a result, I modified the script so that now with each change, the jdk selected by script is 'saved'. You have to add the REG (be careful , only in one line) line as below by adapting the right path of the jdk to your configuration:
@echo off
set JAVA_HOME = C: \ OpenJDK-15
set Path =% JAVA_HOME% \ bin;% Path%
REG ADD "HKEY_LOCAL_MACHINE \ SYSTEM \ ControlSet001 \ Control \ Session Manager \ Environment" undefined v "JAVA_HOME" undefined t "REG_SZ" undefined d "C: \ OpenJDK-15 \\" undefined f
echo Java 15 activated.
be careful too, no space in script with \ and %
Great idea JavaMan
Could you please share the correct "reg add" command ? I think it will help.
What means thes "undefined" string in the REG ADD command ?
I've tried with this :
REG ADD "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Session Manager\Environment" undefined v "JAVA_HOME" undefined t "REG_SZ" undefined d "C:\Program Files\OpenJDK\openjdk-8u262-b10\" undefined f
But the commande is not working. do you see something wrong here ?
Thank you
I love you. This was the easiest thing to follow.
Thanks - happy that I could help :)
actually the easiest way is to use https://github.com/shyiko/jabba which gives you a hand on version selection, installation and auto switching, all the things that a good java version manager can provide.
cheers!