Using SDKMAN! to work with multiple versions of Java

March 04, 2020
Written by

Using SDKMAN! to work with multiple versions of Java

If you code in Java, you might be coding against Java 11, or 8, or maybe an even older version. You may also be prototyping code against newer versions of Java, the current version is 13. Java 14 will be released this month, and 15 later this year. At the same time, you might be investigating different builds of OpenJDK - there are several free alternatives. I use AdoptOpenJDK’s builds of OpenJDK which is an increasingly popular choice according to the JVM Ecosystem Report 2020.

You could also be trying out different build tools like Maven and Gradle. I use both, depending on the project.

Put bluntly, managing all this can be difficult. It’s not impossible to manage by hand, but it’s fiddly and if you get it wrong the error messages can be hard to understand.

Enter SDKMAN! It’s a tool for managing the installation and selection of Software Development Kits - not just different versions and builds of Java itself, but tools for building, debugging, monitoring, documenting and deploying too. It is available for Windows, Linux and MacOS.

 

SDKMAN! logo

 

Installing SDKMAN!

Follow the installation instructions for your platform. If you use the terminal a lot, I highly recommend checking out Oh My Zsh which can add a ton of useful information and behaviour. There is an SDKMAN! plugin for Oh My Zsh which adds tab completion to the sdk command which is really helpful. Enable this by finding the plugins= line in the .zshrc file in your home directory and adding sdk. Mine reads:

plugins=(git sdk)

If you do edit that file you’ll need to reload the Zsh config with source ~/.zshrc.

Installing a specific version of Java

Let’s say we want to install the latest build of Java 11 from AdoptOpenJDK. First of all let's check that it’s available, using sdk list java. If you’ve set up Oh My Zsh this is tab-completable:

Aminated gif showing a shell session where I run "sdk list java" and show that 11.0.6.hs-adpt is in the output, along with about 50 other possible choices.

So the latest Java 11 OpenJDK build from AdoptOpenJDK has the identifier 11.0.6.hs-adpt. Install it with:

sdk install java 11.0.6.hs-adpt

SDKMAN! will download that version of Java, and unzip it into a directory on your computer. You don’t need to worry about exactly where, because SDKMAN! will also update environment variables so that you can use java in the terminal immediately:

➜  java -version
openjdk version "11.0.6" 2020-01-14
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.6+10)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.6+10, mixed mode)

Switching between Java versions

I’ve also installed the latest early-access version of Java 14 (due to be released March 17th 2020) so that I can try out some of the new features. I did that with:

sdk install java 14.ea.36-open

I said “no” when it asked if I wanted to use that as the default version. Later on, switch between them using sdk use java <version>:

Animated gif of a terminal session where I run "java -version" and it shows 11.0.6, then "sdk use java 14.ea.36-open" and "java -version" again. The version is now reported as "14"

I think you would agree that this is a lot easier than managing downloads, installations and environment variables by hand.

You can see what version you are using right now with sdk current:

➜  sdk current                
Using:
java: 14.ea.36-open

Installing other tools

For different projects I use either Maven or Gradle as my build tool, and for debugging and profiling I sometimes use VisualVM. Install the latest versions of these with:

sdk install maven 3.6.3
sdk install gradle 6.2.2
sdk install visualvm 2.0

And they’re instantly available.

Using SDKMAN! with an IDE

To select a particular version of Java for a project in IntelliJ IDEA or Eclipse, you will need to know exactly where SDKMAN! has unpacked your Java installations. This is $HOME/.sdkman/candidates/java, with subdirectories named after the versions. The currently selected version of Java will also be available as current in that directory:

➜  ls -l ~/.sdkman/candidates/java
total 8
drwxr-xr-x 10 mjg mjg 4096 Jan 15 12:14 11.0.6.hs-adpt
drwxrwxr-x  8 sdk mjg 4096 Mar  4 12:43 14.ea.36-open
lrwxrwxrwx  1 mjg mjg   52 Mar  4 12:35 current -> /home/mjg/.sdkman/candidates/java/14.ea.36-open

I recommend keeping things clearer by configuring projects in your IDE to use a specific installation, rather than “current”.

For IntelliJ IDEA this can be done from the “Project Structure” dialog. Either choose an existing version from the drop-down, or add a new Java version by selecting “New…” from the “Project SDK” section:

Screenshot of IntelliJ IDEA Project Structure dialog, highlighting the "New..." button for adding a new installation of Java for this project.

For Eclipse, this is done by heading to “Project Properties”, then “Java Build Path”, “Libraries” and selecting “Modulepath”:

Screenshot of an Eclipse Project configuration, highlighting the sections described in the text: Java Build Path, Libraries, and ModulePath

You should select the version that’s there already (11.0.6.hs-adpt in this example) and remove it, then select “Modulepath” again, “Add Library” and select “JRE System Library” from the dialog:

Another screenshot of Eclipse, this time the "Add Library" dialog, highlighting "JRE System Library"

In the “Add Library” dialog, select “Alternate JRE” and click “Installed JREs” to open another dialog which lets you choose an existing version, or add a new Java installation by browsing to where it is installed:

Another screenshot of Eclipse. This time it&#39;s the "Add Library" dialog with "Alternate JRE" and "Installed JREs" buttons highlighted.

Happy Hacking!

Using SDKMAN! can save you a lot of time and trouble if you work with multiple versions of Java and related tools. Maybe use that time to check out how to build cool things with Java and Twilio like a WhatsApp bot which does image recognition or sending daily SMS reminders?

I’d love to hear what you’re building, tell me about it on Twitter @MaximumGilliard or by email on mgilliard@twilio.com, or post it on Reddit to /r/twilio for the whole world to see.