Switching between multiple JDK versions in Windows

As a java developer, sometimes you will have to maintain multiple JDKs in your computer. You will have to switch between them from time to time for different purposes. Each time you will have to modify your java path according to the JDK version that you are going to use. This can be a hassle when you have to do it multiple times. Its important to setup a mechanism to easily switching between multiple JDK versions in Windows.

You can create a batch file similar to what I have shown below and use them to switch between JDK versions easily.

@echo off
echo Setting JAVA_HOME
set JAVA_HOME=C:\Program Files\Java\jdk-13
echo setting PATH
set PATH=%JAVA_HOME%\bin;%PATH%
echo Display java version
java -version

Replace the JDK installed directory according to the place where you have installed the JDK in your computer and save it as a .bat file. You can download the batch file from https://github.com/dilanthas/Switch-JDK-batch-scripts

Here i have saved this as jdk13.bat. When I run that in the command line it will switch my current JDK to 13.

Also, i have copied the same file and updated the JAVA_HOME with my JDK 8 installation directory and saved it as jdk8.bat. So whenever I want to switch between JDK 8 and 13, I will run this bat file.

Important

Please note that this method will update the JDK only in the current command line you have opened and run the bat file. If you open another command line and try java -version you will still see your older version

Leave a Reply

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