Using SDKMAN! to work with multiple versions of Java
Time to read: 3 minutes
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.
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:
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:
So the latest Java 11 OpenJDK build from AdoptOpenJDK has the identifier 11.0.6.hs-adpt
. Install it with:
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:
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:
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>
:
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
:
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:
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:
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:
For Eclipse, this is done by heading to “Project Properties”, then “Java Build Path”, “Libraries” and selecting “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:
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:
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.
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.