How to switch Java versions in Windows - feature image

How to switch Java versions in Windows (updated 03/2021 for Java 16)

by Sven WoltmannMarch 16, 2021

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.

Installation of multiple Java versions

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.

Download sources

  • Java SE 1.1 – You can no longer install this version on 64-bit Windows.
  • Java SE 1.2 – Installed to 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.
  • Java SE 1.3 – Installed to C:\jdk1.3.1_28\ by default – I recommend changing this to C:\Program Files (x86)\Java\jdk1.3.1_28\.
  • Java SE 1.4 – Installed to 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:

Environment variables

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

Setting environment variables manually

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:

Searching for "env"
Searching for "env"

At this point, you can press "Enter," and the system properties appear:

Windows 10 System Properties
Windows 10 System Properties

Here you click on "Environment Variables…", after which the following window opens:

Windows 10 Environment Variables Java 16
Windows 10 Environment Variables Java 16

As the standard version, I currently use the latest release version, Java 16. Therefore, you should make the following settings:

  • The top list ("User variables") should not contain any Java-related entries.
  • The lower list ("System variables") should contain an entry "JAVA_HOME = C:\Program Files\Java\jdk-16\" (please check the version number, you may have a newer one). If this entry does not exist, you can add it with "New…". If it exists, but points to another directory, you can change it with "Edit…".
  • Delete the following entries under "Path":
    • C:\ProgramData\Oracle\Java\javapath
    • C:\Program Files (x86)\Common Files\Oracle\Java\javapath
  • Insert the following entry instead:
    • %JAVA_HOME%\bin

The path list should then look like this:

Adding "%JAVA_HOME%\bin" to the "Path" system variable
Adding "%JAVA_HOME%\bin" to the "Path" system variable

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:

Checking the Java Environment Variables
Checking the Java Environment Variables

Scripts for changing the Java version

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, java16, java15, java14, java13. I put these commands in the directory C:\Program Files\Java\scripts, which I add to the environment variable "Path" as described before:

Adding "C:\Program Files\Java\scripts" to the "Path" system variable
Adding "C:\Program Files\Java\scripts" to the "Path" system variable

The files are named, for example, java16.bat, java15.bat, java14.bat, java13.bat, and change the environment variables as shown in the following example of java16.bat:

@echo off
set JAVA_HOME=C:\Program Files\Java\jdk-16
set Path=%JAVA_HOME%\bin;%Path%
echo Java 16 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 ZIP file contains all my scripts from Java 1.2 to Java 17 (you might have to adopt one or the other Java directory to your installation):

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.

Switching the Java Version
Switching the Java Version

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.

Free Bonus:

Big O Cheat Sheet

[7 Time Complexity Classes on 1 Page]

Use this 1-page PDF cheat sheet as a reference to quickly look up the seven most important time complexity classes (with descriptions and examples).

You get access to this PDF by signing up to my newsletter. I won't send any spam, and you can opt out at any time.
Invalid email address
Sven Woltmann
About the author
I'm a freelance software developer with more than two decades of experience in scalable Java enterprise applications. My focus is on optimizing complex algorithms and on advanced topics such as concurrency, the Java memory model, and garbage collection. Here on, I want to help you become a better Java programmer. Read more about me here.

Leave a Reply

Your email address will not be published. Required fields are marked *

30 comments on “How to switch Java versions in Windows (updated 03/2021 for Java 16)”

  1. 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?

    1. Try creating files in some other directory (say D drive) and then move those files to java folder. While moving, Windows will show a Administrator dialog box, click YES.

  2. 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?)

  3. 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!

  4. 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?

  5. 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".

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

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

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

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

  8. Thanks for the informative guide.
    One strange thing that happened to me is because I had folders with special characters in my path variable (C:\Adb&FastBoot), set Path=%JAVA_HOME%\bin;%Path% would still work but also echo Fastboot is not a recognized command. Probably breaking some other things after the C:\Adb&FastBoot.
    Altering the switching scripts from set Path=%JAVA_HOME%\bin;%Path% to set "Path=%JAVA_HOME%\bin;%Path%" sorted things out.

  9. PowerShell version of the script

    $Env:JAVA_HOME="C:\Program Files\Java\jdk-11.0.5"
    Write-Host Java 11 activated.

  10. Delete the following entries under "Path":
    C:\Program Files (x86)\Common Files\Oracle\Java\javapath

    Why are we deleting these two?

    1. Because we want to use the Java executables in %JAVA_HOME%\bin; not in C:\ProgramData\Oracle\Java\javapath or C:\Program Files (x86)\Common Files\Oracle\Java\javapath.

  11. I didn't set it up on the Windows 10 Server but I think that when it starts as a service using Tomcat9.exe it is ignoring the environment variable and using the -auto option to locate the JVM in the registry. I am not a Windows aficionado so I am not sure what the registry key would be and there are only about 1500 of them.

    1. Hey Don, I've never set up Tomcat as a Service on Windows. But usually, Tomcat has a setenv.bat file in the bin directory, where you can set the JAVA_HOME environment variable.

  12. This has been one of the most fruitful topics I have read and implemented in a long time. This problem of working on multiple Java versions simultaneously has been haunting me for a long time. The solution provided is cute, simple and it is going to save several hours of misery for me. Thanks

You might also like the following articles