Using SDKMAN! to work with multiple versions of Java
Tags: java, installation, tools
Published: 2020-03-06 11:32:01

---
title: Using SDKMAN! to work with multiple versions of Java
published: true
tags: java, installation, tools
canonical_url: https://www.twilio.com/blog/sdkman-work-with-multiple-versions-java?utm_source=devangel&utm_medium=social&utm_campaign=matthew
---

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](https://adoptopenjdk.net/) which is an increasingly popular choice according to the [JVM Ecosystem Report 2020](https://snyk.io/blog/36-of-developers-switched-from-oracle-jdk-to-an-alternate-openjdk-distribution-over-the-last-year/).

You could also be trying out different build tools like [Maven](https://maven.apache.org/) and [Gradle](https://gradle.org/). 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!](https://sdkman.io/) It’s a tool for managing the installation and selection of Software Development Kits - not just [different versions and builds of Java](https://sdkman.io/jdks) itself, but tools for [building, debugging, monitoring, documenting and deploying](https://sdkman.io/sdks) too. It is available for Windows, Linux and MacOS.

 

![SDKMAN! logo](https://twilio-cms-prod.s3.amazonaws.com/images/Selection_191.width-500.png)

 

## Installing SDKMAN!

Follow the [installation instructions](https://sdkman.io/install) for your platform. If you use the terminal a lot, I highly recommend checking out [Oh My Zsh](https://ohmyz.sh/) 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.](https://twilio-cms-prod.s3.amazonaws.com/original_images/1GDYeEXxYhWuPkNIFdTI7rw8AiSBCJwGFYuHN-3ghbGglpYiEROI0yqHlIIxx90QmMX-GMVf1w4R_h2OzfAy)

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"](https://twilio-cms-prod.s3.amazonaws.com/original_images/gvym4s1R1Xeh_HMuXB66ORa8ymgoPdFj-7I4QOVWaNxOigsmqMrOMBH78L3d3zLraYk80zptdqV3_Wkq3ccs)

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](https://maven.apache.org/) or [Gradle](https://gradle.org/) as my build tool, and for debugging and profiling I sometimes use [VisualVM](https://visualvm.github.io/). 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.](https://twilio-cms-prod.s3.amazonaws.com/images/G__Cbbdtuvl2ffqe_v08XegrD_w8vU-mi6MUT3qEvhzCbs.width-500.png)

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](https://twilio-cms-prod.s3.amazonaws.com/images/pVRhbITDt9oomMb8ajqNo9CQeIYmPPCoqdd6YWa5Kcf-ff.width-500.png)

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"](https://twilio-cms-prod.s3.amazonaws.com/images/L1b2WCQdrOomPFfGzDzBfdPRqWoFy_Hr8EJzL8CRBstDsm.width-500.png)

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.](https://twilio-cms-prod.s3.amazonaws.com/images/ag2OSKIuKEqUP9SZIujceVClBPDxJ32AKpQJW00dkRg3PS.width-500.png)

## 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](https://www.twilio.com/blog/tag/java) like a [WhatsApp bot which does image recognition](https://dev.to/twilio/using-whatsapp-twilio-and-azure-to-generate-photo-alt-text-in-java-1m97-temp-slug-4791396) or [sending daily SMS reminders](https://dev.to/mjg123/wake-up-to-useful-daily-messages-with-java-and-azure-functions-4d0k-temp-slug-9592645)?

I’d love to hear what you’re building, tell me about it on Twitter [@MaximumGilliard](https://twitter.com/MaximumGilliard) or by email on [mgilliard@twilio.com](mailto:mgilliard@twilio.com), or post it on Reddit to [/r/twilio](https://reddit.com/r/twilio) for the whole world to see.