Manage Multiple JDKs In Windows

Search for a command to run...

Nice article!
In my opinion the best way to manage Java versions is using the scoop.sh tool.
With Scoop you must to add de Java bucket and then install any version you want, as explained here: https://github.com/lukesampson/scoop/wiki/Java
You can use sdkman.io but will need to install WSL, Cygwin or other tool like Git Bash for Windows.
Agree, This seems a more a cleaner way to manage multiple Java installations in Windows.
I will try it and update the article appropriately.
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.
C drive or in C:\java directory.Here I assume that you are aware of the windows environment variable settings.
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.

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

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)
After successfully setting up one version of java, now it's time to write one-liner batch scripts to switch the JDK versions.
open-jdk-15.bat file and your Java installation path.SETX JAVA_HOME "C:\tools\java\jdk-15"
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.
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.
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...