How to switch Java versions in Windows - feature image

How to Change Java Versions in Windows (Updated for Java 17)

by Sven WoltmannSeptember 14, 2021

In this article, I will show you how to install multiple Java versions on Windows and how to change the Java version on the command line:

Multiple Java versions on Windows

To enable these Java version change commands on your system as well, follow this step-by-step guide.

Let's go…

Step 1: Installing Multiple Java Versions

Installing multiple Java versions in parallel 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:

Step 2: Define Java Environment Variables

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

Manually Setting the Java Environment Variables

The Java installers 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:

Opening Windows environment variables
Opening Windows environment variables

At this point, you can press "Enter" to open the system properties:

Windows 10 System Properties
Windows 10 System Properties

Click on "Environment Variables…" and the following window opens:

Windows environment variables Java 17
Windows environment variables Java 17

As the default version, I recommend the current release version, Java 17. Accordingly, 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-17". 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 entry should then look like the following (the other entries in the list will probably look different for you since you have other applications installed than I do):

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. But don't worry – the scripts you can download in the next step will do that automatically.

How to Check Your Java Version on Windows

Now open a command line to check the settings with the following commands:

echo %JAVA_HOME% java -version
Code language: plaintext (plaintext)

Here's what you should see:

Check your Java version with "cmd"
Check your Java version with "cmd"

Step 3: Install the Scripts to Change the Java Version

To change the Java version on the command line, I have prepared some batch files that you can copy to your system. Here is the link:

The ZIP file contains scripts named java18.bat, java17.bat, java16.bat, etc., for all Java versions. I suggest you unpack the scripts to C:\Program Files\Java\scripts.

The scripts look like this (java17.bat):

@echo off set JAVA_HOME=C:\Program Files\Java\jdk-17 set Path=%JAVA_HOME%\bin;%Path% echo Java 17 activated.
Code language: DOS .bat (dos)

The script updates the JAVA_HOME environment variable and inserts the bin directory at the beginning of the Path variable. That makes it the first directory to be searched for the corresponding executable when you run Java commands such as java or javac.

(The Path variable gets longer with each change. Do not worry about it. This only affects the currently opened command line.)

Step 4: Add the Script Directory to the Path

To be able to call the scripts from anywhere, you have to add the directory to the "Path" environment variable (just like you did with "%JAVA_HOME%\bin" in the second step):

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

If you have installed the latest releases of all Java versions, you can use the scripts without any further adjustments. Open a new command line and enter, e.g., the following commands:

Changing the Java version
Changing the Java version

If one of the commands does not activate the expected Java version, please check if the path in the batch file corresponds to the installation path of the Java version you want to activate.

Temporary, Permanent, and System-Wide Java Version Changes

The commands presented up to this point only affect the currently opened command line. As soon as you open another command line, the default version defined in step 2 is active again (Java 17, if you have not changed anything).

That is why there are not one but three scripts for each Java version:

  • java<version>: Activates the Java version in the current command line.
  • java<version>-user: Sets the Java version as the default version for your user account.
  • java<version>-system: Sets the Java version as the default version for the entire system-

The -user variants of the scripts additionally set the JAVA_HOME environment variable with the setx command, permanently writing the change to the registry:

@echo off set JAVA_HOME=C:\Program Files\Java\jdk-17 setx JAVA_HOME "%JAVA_HOME%" set Path=%JAVA_HOME%\bin;%Path% echo Java 17 activated as user default.
Code language: DOS .bat (dos)

The -system variants also specify the /M parameter in the setx command. This sets the system-wide environment variable instead of the user-specific one:

@echo off set JAVA_HOME=C:\Program Files\Java\jdk-17 setx JAVA_HOME "%JAVA_HOME%" /M set Path=%JAVA_HOME%\bin;%Path% echo Java 17 activated as system-wide default.
Code language: DOS .bat (dos)

Attention: To set the system-wide Java version, you must open the command line as an administrator. Otherwise, you will get the error message "ERROR: Access to the registry path is denied.

What You Should Do Next…

I hope you were able to follow the instructions well and that the commands work for you.

Now I would like to hear from you:

Were you able to follow the steps well – or do you have unanswered questions?

Either way, let me know by leaving a comment below.

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 *

47 comments on “How to Change Java Versions in Windows (Updated for Java 17)”

  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. Hi Daniel, the scripts from the first version of this article worked only in the command line, in which you executed them. The latest version of the article now contains additional scripts to change the user and system defaults (see chapter "Temporary, Permanent and System-Wide Java Version Changes").

      Or are you speaking of Java applets? Support for applets has been removed with Java 11, and I haven't worked with them in years, so I don't know if there's a way to change the Java version used for applets.

    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!

    1. Hi Chris, I have released a new set of scripts that also contain commands to switch the Java version permanently (see section "Temporary, Permanent and System-Wide Java Version Changes").

  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?

    1. Hi sainath, in gitbash, you have to write the full file name of the script, e.g. java11.bat.

    1. Hi Sashi, use the "...-user" or "...-system" commands described in the section "Temporary, Permanent and System-Wide Java Version Changes".

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

    2. Instead of using "reg add", you can use "setx". It's shorter and has an immediate effect, while the changes made with "reg add" take several minutes, sometimes even a restart to be effective.

      I've added new commands (ending with "-user" and "-system") to set the user and system-wide default Java version permanently (see section "Temporary, Permanent and System-Wide Java Version Changes").

  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

  13. nice solution for switching versions, i was trying to merge all the version scripts in one bat file but and added a if else statement i am not able to run that bat file by default in my command prompt. Any idea

  14. I'm trying to switch between Java 8 and Java 16. In the terminal I use the commands in ("java8"/"java16"), it confirms that my version has been switched, but as soon as I close and open a new terminal, it is still the version I tried to switch off of. I have very little experience with this stuff, and any help would be greatly appreciated.

    1. Hi Chase, you can use the recently added commands "java8-user" and "java16-user" (for example) to set the Java version permanently for your user account (see the newly added section "Temporary, Permanent and System-Wide Java Version Changes").

  15. Hi, would like these scripts if they worked, had to change what the java8 script goes to because it went for jdk1.8.0_231 while the actual was jre1.8.0_301.

    This is fine, however, the script doesn't work properly with -user suffix, given, my computer seems to be special in how it does things. The user variable Path has one value, and it goes to %USERPROFILE%\AppData\Local\Microsoft\WindowsApps
    If there is a better value or values for it, please respond with such or at least one for JAVA_HOME variable for User to work.

  16. Nice work, I did one change though I merged them all into one and used the %1 %2 to indicate the version I was interested in running and if I wanted it user or system besides that excellent work. Thanks for taking the time to make the tutorial.

  17. Fantastic Guide, I think it is one of the best. And as I wanted diffrent versions that all of the scripts offer. Fantastic work Sven ;)

    Best regards Carl Josef

  18. I observed an issue while attempting to switch between Java versions in Windows 10.

    Even after setting JAVA_HOME and the corresponding PATH entry you may still see older/previous version of Java/jdk upon running java -version.

    To fix this you'll need to remove the entry C:\Program Files (x86)\Common Files\Oracle\Java\javapath from PATH and make sure you have %JAVA_HOME%\bin entry under PATH and corresponding JAVA_HOME defined.

    More info:

You might also like the following articles