Managing Multiple JDK Versions with SDKMAN!

In today’s software development landscape, working with multiple Java Development Kit (JDK) versions is not uncommon. Different projects may require different JDK versions due to compatibility issues, language features, or performance optimizations. Managing these JDK versions manually can be cumbersome and error-prone. That’s where SDKMAN! comes to the rescue!

What is SDKMAN!?

SDKMAN! is a tool that simplifies managing multiple software development kits (SDKs) on your system. It provides a convenient way to install, manage, and switch between different versions of JDKs, JVMs, build tools, and more. With SDKMAN!, you can effortlessly handle different JDK versions without the hassle of manual installation and configuration.

Getting Started with SDKMAN!

Before diving into managing JDK versions, let’s first set up SDKMAN! on your system. Follow these steps to get started:

Step 1: Installation

Just launch a new terminal and type in:

$ curl -s "https://get.sdkman.io" | bash

Follow the on-screen instructions to wrap up the installation. Afterward, open a new terminal or run the following in the same shell:

$ source "$HOME/.sdkman/bin/sdkman-init.sh"

Lastly, run the following snippet to confirm the installation’s success:

$ sdk version

or run

sdk help
    sdk - The command line interface (CLI) for SDKMAN!

SYNOPSIS
    sdk <subcommand> [candidate] [version]

DESCRIPTION
    SDKMAN! is a tool for managing parallel versions of multiple JVM related
    Software Development Kits on most Unix based systems. It provides a
    convenient Command Line Interface (CLI) and API for installing, switching,
    removing and listing Candidates.

SUBCOMMANDS & QUALIFIERS
    help         [subcommand]
    install      <candidate> [version] [path]
    uninstall    <candidate> <version>
    list         [candidate]
    use          <candidate> <version>
    config       no qualifier
    default      <candidate> [version]
    home         <candidate> <version>
    env          [init|install|clear]
    current      [candidate]
    upgrade      [candidate]
    version      no qualifier
    offline      [enable|disable]
    selfupdate   [force]
    update       no qualifier
    flush        [tmp|metadata|version]

EXAMPLES
    sdk install java 17.0.0-tem
    sdk help install

Step 2: Installing JDKs

Now that SDKMAN! is set up, you can start installing JDK versions. Use the following command to list available JDK candidates:

sdk list java

This command will display a list of JDK versions that you can install using SDKMAN!. Choose the desired version and install it by running:

sdk install java <version>

Replace <version> with the JDK version you want to install, such as 11.0.13-zulu or 16.0.2-open.

Step 3: Switching JDK Versions

Once you have multiple JDK versions installed, you can easily switch between them using SDKMAN!. Use the following command to list installed JDK versions:

sdk list java

Identify the version you want to use and set it as the default by running:

sdk default java <version>

Replace <version> with the desired JDK version.

You can switch to a specific JDK every time you open a project, This can be done through .sdkmanrc file in the base directory of your project. This file can be generated automatically by issuing the following command:

sdk env init

A config file with the following content has now been created in the current directory:

# Enable auto-env through the sdkman_auto_env config
# Add key=value pairs of SDKs to use below
java=21.0.2-tem

The file is pre-populated with the current JDK version in use but can contain as many key-value pairs of supported SDKs as needed. To switch to the configuration present in your .sdkmanrc file, simply issue the following command:

sdk env

You will get an output that looks like this:

Using java version 21.0.2-tem in this shell.

Your path has now also been updated to use any of these SDKs in your current shell. When leaving a project, you may want to reset the SDKs to their default version. This can be done with this command

$ sdk env clear
Restored java version to 21.0.2-tem (default)

To read more about sdkman check the official website: Home – SDKMAN! the Software Development Kit Manager

Leave a comment