Managing multiple JDKs on macOS with SDKMan

Published by

on

Managing multiple versions of JDK on macOS could be a fairly daunting task especially when you could be working on multiple projects requiring different JDK versions. You could manually update the PATH & JAVA_HOME environment variables everytime you need to switch JDK version or you could also simply use /usr/libexec/java_home (see this article) however, a more elegant and simpler way is to install and use SDKMan!

So, first things first, head over to SDKMan and follow the instructions to install it. Once done. open up a terminal and issue

sdk list java

to see all the versions of JDK available. Once you’ve identified your desired version, issue –

sdk install java 21.0.2-zulu

This will install JDK 21.0.1-zulu from Azul Systems. You can replace the version with your desired version. Remember that the first version you install becomes the default version to be used on new terminals. You can install all the desired versions by simply replacing the version.

To switch to another version of JDK, simply issue

sdk use java 11.0.22-zulu

This will switch the JDK to 11.0.22-zulu for your current terminal. If you prefer to make this the default JDK version for your terminals, issue

sdk default java 11.0.22-zulu

Now everytime you open a new terminal, 11.0.22-zulu would be the default JDK version.

That’s not the only thing you can do with SDKMan though. Developers coming from Python world know that there’s venv or conda utilities that setup a virtual environment in which you can install all the relevant libraries and even set a specific version of Python to use. Well, SDKMan can do this as well. The advantage is that you can switch to a specific JDK or SDK every time you visit a project! This file can be done by issuing

sdk env init

What this command does is it creates .sdkmanrc file in the base directory of your project. The file is pre-populated with the current JDK version in use; however, it can contain as many key-value pairs of supported SDKs as needed. Now everytime you need to work on this project, simply issue

sdk env

when you switch (cd) to your project in a terminal and the configured JDK version will be made available to your terminal. No more accidently compiling projects with the wrong version! When you’re done with your project simply close the terminal or if changing to another folder or project simply clear to default JDK by issuing

sdk env clear

Not only can you manage JDKs but also install many SDKs such as Karaf, Groovy, Hadoop etc. Checkout SDKs available here. Enjoy the simplicity of SDKMan.

Discover more from EZEETECH AI

Subscribe now to keep reading and get access to the full archive.

Continue reading