How to switch Java versions in Windows - feature image

How to switch Java versions in Windows (updated 09/2020 for Java 15)

by Sven Woltmann – September 15, 2020

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 15
Windows 10 Environment Variables Java 15

As the standard version, I currently use the latest release version, Java 15. 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-15\” (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, 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:

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, 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):

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.


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.

You might also like the following articles
Leave a Comment

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

  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?

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

  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.

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}