Manage Multiple JDKs In Windows

Manage Multiple JDKs In Windows

Since when Oracle Adopted 6 monthly release cycle for Java, It is most likely that Java developers have to deal with multiple versions of JDKs.

Considering the diversity of the Java community, most of the Java libraries and framework supports at least LTS versions of the Java. Still, there are many active Java open-source and commercial projects are using Java 8 and other Java version.

In this situation, you may have to manage multiple installations of the JDK in your system. In Linux, Handling multiple JDKs is fairly easy and there are tools available that can do this job for you.

For windows, the situation is different. There are no open-source free tools available that can seamlessly handle multiple JDK installations. I have spent several hours looking for available solutions, but no luck.

Finally, I have to write custom scripts that can handle multiple JDKs for me. Let me walk you through the installations of JDKs and Switching between them.

I have used 2 versions of Java (OpenJDK 8 and OpenJDK 15) in this article.

Setup Multiple JDKs In Windows

  • Download Java 8 and Java 15 or any of the 2 JDK versions of your choice. Download OpenJDK zip files, which are easy to manage.
  • Extract the zip into the root of the C drive or in C:\java directory.

Setup JAVA_HOME Environment Variable and Java In System Path

Here I assume that you are aware of the windows environment variable settings.

  • Setup JAVA_HOME user variable. For 1step use of any 1 JDK path. You can use the control panel or command prompt with the following command.
setx JAVA_HOME C:\tools\java\jdk-15

Note: This path is just for reference. Use your java installation path instead.

Notice that JAVA_HOME does not contains /bin directory. Many build tools like maven and Gradle require JAVA_HOME in this format.

setup-java-home-environment-variable-windows.png

  • Now let's set up java and javac in the path environment variable. Here, we will reference the JAVA_HOME value in the path variable.

Note: As updating system variables need administrative privileges, I have setup JAVA_HOME and path variable in user variables.

%JAVA_HOME%\bin

setup-java-path-windows.png

  • To test the java installation, run the following command in the new command prompt window.
java --version

You should get output similar to this.

openjdk 15 2020-09-15
OpenJDK Runtime Environment (build 15+36-1562)
OpenJDK 64-Bit Server VM (build 15+36-1562, mixed mode, sharing)

Setup Batch Scripts to Switch JDKs

After successfully setting up one version of java, now it's time to write one-liner batch scripts to switch the JDK versions.

  • Create open-jdk-15.bat file and your Java installation path.
SETX JAVA_HOME "C:\tools\java\jdk-15"
  • Create another batch file open-jdk-8 and use your Java 8 installation path in the script.
SETX JAVA_HOME "C:\tools\java\java-se-8u41-ri"

Similarly, you can create many batch files. One per JDK.

Switch Between JDKs

Now I guess you guys get my idea. You just need to execute the batch file of the JDK which you want to use and re-open the command prompt windows and check the java version.

java --version

You should able to see different Java versions now.

Conclusion

We have discussed the strategy to manage multiple JDK in Windows OS. The idea is simple. We have set up the JAVA_HOME variable and referenced it in the path. And by switching the JAVA_HOME value using the batch script we can easily switch the JDK version.

Let me know your way to handle multiple JDKs in windows or If you liked this article then please feel free to share it with your team and community.

Until Next Article, Happy Coding...

Did you find this article valuable?

Support Akshay Pethani by becoming a sponsor. Any amount is appreciated!