How to switch between Multiple JDKs (JAVA) on Windows
Switching between Multiple JDKs (JAVA) on Windows from the command line

As a Java programmer, we may work on various projects and tools like Jenkins on different Java versions, especially since the transition to the six-month release cycle.
The typical way is going to Environment variables and change the bin path according to the particular java version. You may find that this is repetitive work and we have to change back it to the default Java version manually every time we change it.
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 the Environment Variables window every time. So it is helpful to be able to quickly and easily change the Java version to be used for compiling code, launching applications or executing class files.
So let's see how to do it…

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.
In my machine, I have installed the JDK versions 8, 14, 15, 16. I will put the links to download the JDK files below.
JDK 8u202 Java SE 14 Java SE 15 Java SE 16
Make sure you install all the JDKs in the same folder.
Setting Environment variables
Our windows system decides the java version we are using by the following Environment Variables.
JAVA_HOMEPath
These variables should always point to the same Java installation to avoid unforeseen problems due to inconsistencies.
In my case, I have set the latest version JDK 16 as my default Java path


Now open a command line to check the settings with the following command. It should be set up for the JDK 16
java -version

Scripts for changing the Java version
So here we come to the scripting part. In the same folder, you have installed your JDKs create a folder named “scripts” and set the path of that folder in the Environment Variables.

create four .bat files according to the JDK versions and put this script in each one declaring the JAVA_HOME.

@echo off
set JAVA_HOME=C:\Program Files\Java\java-se-8u41-ri
set Path=%JAVA_HOME%\bin;%Path%
echo Java 8 activated.The above example shows the script for the JDK 8.
Now you just have to type the batch file name and the JDK version will change accordingly.

THINK QUALITY!





