Install and run multiple Java versions on Linux and MacOS | by Md Kamaruzzaman | Towards Data Science

If you are a Java developer, you may need to install multiple Java versions on your machine. You may use Java8 or Java11 in your project, but want to learn newer versions of Java. Or maybe you are working on two different projects where two different versions of Java are used.
One way to achieve this is to install multiple Java versions and configure different Java versions manually. In that case, you have to modify a few environment variables when you need to switch your Java version. The other elegant and convenient way is to use the SDKMAN.
SDKMAN is a Software Development Kit Manager for managing parallel versions of multiple Software Development Kits on most Unix based systems. It allows us to install, remove, switch and list candidate versions of different SDKs including Java (e.g., JDK, Ant, Dotty, Gradle, Scala, and many more).
Here I will show how to manage multiple Java version on your UNIX based machine using SDKMAN. By reading this article, you will learn the following:
Install SDKMAN
List candidate JDKs using SDKMAN
Install multiple candidate JDKs using SDKMAN
Switch candidate JDKs using SDKMAN
Remove/Uninstall JDK using SDKMAN
The instructions are valid for most of the UNIX based operating systems including MacOS, any Linux OS (Ubuntu, Debian, MINT, Manjaro, OpenSUSE), FreeBSD, Solaris, Cygwin.
Open a new terminal and enter
curl -s “https://get.sdkman.io" | bash
Now, copy & paste the following in the terminal and enter
source "$HOME/.sdkman/bin/sdkman-init.sh"
It will install the SDKMAN in your machine. You can then check the version of the SDKMAN
sdk version
See the available Java versions offered by SDKMAN by giving the following command
sdk list java
It will show all the available JDK from different vendors with supported versions.
Install a particular JDK, e.g., JDK 15 Liberica from BellSoft
sdk install java 15.0.0-librcaInstalling: java 15.0.0-librcaDone installing!Do you want java 15.0.0-librca to be set as default? (Y/n):
You can now check the installed Java version with the following command
java -versionopenjdk version "15" 2020-09-15OpenJDK Runtime Environment (build 15+36)OpenJDK 64-Bit Server VM (build 15+36, mixed mode, sharing)
You can now install another JDK, e.g., JDK 8 Liberica from BellSoft
sdk install java 8.0.265-librcaInstalling: java 8.0.265-librcaDone installing!Do you want java 15.0.0-librca to be set as default? (Y/n):
If you now list available JDKs, you will see the installed JDKs marked as installed, as shown below.
sdk list java
You can then change the JDK with the following command
sdk use java 8.0.265-librcaUsing java version 8.0.265-librca in this shell.
You can check the installed JDK as follows
jdk -versionopenjdk version "1.8.0_265"OpenJDK Runtime Environment (build 1.8.0_265-b01)OpenJDK 64-Bit Server VM (build 25.265-b01, mixed mode)
You can uninstall a JDK using the following command
sdk uninstall java 15.0.0-librcaUninstalling java 15.0.0-librca...
In this article, I have shown how easily and elegantly you can manage Multiple Java version in your UNIX based operating system with SDKMAN. I am using NVM to manage multiple Node.js version in my local machine and I would put SDKMAN on par with NVM in terms of usefulness and developer friendliness. In fact, SDKMAN supports installing other JVM tools and SDKs like Groovy, Scala, Kotlin, Ceylon. Ant, Gradle, Grails, Maven, SBT, Spark, Spring Boot, Vert.x. If you work with Java and JVM, you should give SDKMAN a try. It will make your life easier to manage multiple Java and other JVM based SDK versions.