avatarManul Wickramanayaka

Summary

The website provides instructions for switching between multiple JDK versions on Windows using environment variables and batch scripts.

Abstract

The article "How to switch between Multiple JDKs (JAVA) on Windows" addresses the need for Java developers to work with different Java versions for various projects and tools. It explains the repetitive task of manually updating environment variables to switch Java versions and offers a solution to streamline this process. The article guides users through installing multiple Java Development Kit (JDK) versions, setting up environment variables (JAVA_HOME and Path), and creating batch scripts to quickly change the active Java version from the command line without altering the system's default settings. By following these steps, developers can efficiently test code and run applications on different Java versions.

Opinions

  • The author emphasizes the inconvenience of manually changing environment variables each time a different Java version is required.
  • The six-month release cycle of Java has increased the need for a more efficient way to switch between JDK versions.
  • The author believes that having the ability to easily switch Java versions is beneficial for testing code snippets and running applications on the command line.
  • The article suggests that setting up multiple JDKs and creating batch scripts for version switching is a time-saving and practical approach for Java programmers.

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_HOME
  • Path

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

JAVA_HOME
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

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.

Path

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

scripts folder
@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.

Switching between JDK versions

THINK QUALITY!

Tools
Java
Jdk
Switch
Recommended from ReadMedium