How to manage multiple Java Versions on Windows and why updating usual variables might not be enough

Tetiana
4 min readJun 17, 2023

--

If you’ve ever worked with multiple Java versions on a single environment, you’re likely familiar with the challenges it can present. In this article, I’ll share my personal experience and some tricks in setting up an environment with multiple Java releases on Windows.

You might have already seen a lot of guides and tutorials about working with multiple Java versions in a single environment. This was not my first time configuring several Java releases, and I was completely confident in the steps I had taken. I updated the necessary variables, reloaded cmd and bash multiple times, and in the end, I even restarted my PC. However, none of these efforts had any effect, and I was still encountering the old version of Java.

As every Java developer knows, there are two variables that are necessary for Java to work correctly. These variables are installed during the JDK installation and can be updated manually: JAVA_HOME and PATH, you can see them below:

JAVA_HOME
PATH
Path

It’s essential to ensure that both variables point to the same location. In my case, although both variables were correctly configured for Java 17, however the “java -version” command still displayed the outdated Java 11, which was the initially installed version.

PATH java version vs console output java version
PATH java version vs console output java version

Let’s investigate the reasons behind this behavior. It is important to know, that Java 11 was installed using the installation wizard, whereas Java 17 was simply unzipped into the appropriate directory.

During the installation process, a “C:\Program Files\Common Files\Oracle\Java\javapath” directory is created. This directory serves as a symlink and, from what I understood, it points to the latest version installed via the wizard.

This is how content of this folder looks like, and as you can see there is a java.exe present.

C:\Program Files\Common Files\Oracle\Java\javapath files

When we examine the Path variable (check the screenshot above), we see that this directory is present at #2 position. Windows works in such a way, that it will take the first found executable from the Path and will use it to execute command.

To resolve this issue, we have two solutions: either remove “C:\Program Files\Common Files\Oracle\Java\javapath” from Path, or move it below than desired JAVA_HOME/bin directory.

After reloading the terminal, I could see that the Java version had been updated.

java -version when redundantpath value moved below

Windows Bat script

To easily switch between versions within a local session, I utilize small scripts placed in the C:\Program Files\Java\bat directory. This directory is then added to the system Path variable as well.

Bat script for java11.bat:

@echo off
set JAVA_HOME=C:\Program Files\Java\jdk-11.0.15
set Path=%JAVA_HOME%\bin;%Path%
echo Successfully switched to Java 11. java -version output:
echo:
java -version

Bat script for java17.bat:

@echo off
set JAVA_HOME=C:\Program Files\Java\jdk-17.0.7
set Path=%JAVA_HOME%\bin;%Path%
echo Successfully switched to Java 17. java -version output:
echo:
java -version

Below is an example of how you can use this approach to easily switch between versions. Please note that this method is specific to the terminal session, allowing you to keep the main Java version in the Path and JAVA_HOME variables and switch versions only when needed using the executable (java11 or java17 in my case).

In the example below, you can see that on the left side, I have Java 11 set using the script, while on the right side, in another cmd session, I have the version specified in the actual Path. This allows for convenient and seamless switching between different Java versions based on your requirements.

Switching between versions via executables

Hopefully, you have found something useful for you!

--

--