Managing Multiple Java Versions on Linux: Stop Doing It Manually, Use SDKMAN!

Linux tutorial - IT technology blog
Linux tutorial - IT technology blog

The Nightmare of “Java Version Conflicts”

As a Java developer, you’ve likely faced this irony. A legacy project at work runs on Java 8, your personal project needs Java 21, and other microservices require Java 17. The traditional way is downloading .tar.gz files, extracting them to /usr/lib/jvm, and manually configuring JAVA_HOME. This isn’t wrong, but it’s incredibly time-consuming.

If you forget to update your environment variables, you’ll end up building a project with an incompatible version. The result is a barrage of runtime errors. I used to use the update-alternatives command on Ubuntu, but its syntax is cumbersome. Having to type sudo every time you switch versions disrupts your workflow.

SDKMAN! – A Lifesaver for Developers

SDKMAN! (The Software Development Kit Manager) helps you clear up this mess. It is a command-line tool (CLI) that supports installing and switching between SDKs lightning-fast. Besides Java (JDK), it also manages Maven, Gradle, Kotlin, and over 20 other tools. Everything operates at the user level without requiring root privileges.

The biggest plus is the ability to isolate environments. Version switching happens instantly in the current terminal. On the 4GB RAM Ubuntu VPS I’m running, SDKMAN! saves about 15 minutes of setup time for every new project. It doesn’t interfere with or break any system default configurations.

Installing SDKMAN! with Just a Few Commands

First, ensure your machine has curl and unzip. These are two essential tools on Linux. If you don’t have them, install them using:

sudo apt update
sudo apt install curl unzip zip -y

Next, download and install SDKMAN! directly from their server:

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

After the script finishes, you need to activate the environment by running:

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

To verify the installation, type sdk version. If a specific version appears on the screen, you’re ready to manage your JDKs.

Professional JDK Management Operations

1. Searching for the Right Java Version

SDKMAN! provides JDKs from many distributors like Amazon Corretto, Microsoft, or Temurin. To view the list, use the command:

sdk list java

A list will appear. Pay attention to the Identifier column. This is the ID you’ll use to install the exact version you need.

2. Quick JDK Installation

Suppose you need Java 17 from Temurin (currently the most stable version for production). Type:

sdk install java 17.0.10-tem

SDKMAN! will automatically download about 200-300MB of data, extract it, and configure everything. You can install Java 8 for legacy project maintenance with a similar command.

3. Switch Versions in a Second

This is the most valuable feature. If you want to use Java 17 for the current terminal only, type sdk use java 17.0.10-tem. Conversely, to set a version as the default for every time you open the machine, use sdk default java 17.0.10-tem. Check again with java -version, and you’ll see the change immediately.

Pro Tip: Automation with the .sdkmanrc File

Manually typing switch commands can still lead to mistakes. Fortunately, SDKMAN! supports an environment auto-detection feature. In your project’s root directory, run:

sdk env init

This command creates a .sdkmanrc file. Simply enter your desired Java version, for example: java=17.0.10-tem. Every time you cd into this directory, just type sdk env to switch to the correct version.

If you want full automation, open the config file at ~/.sdkman/etc/config. Then, change the value to sdkman_auto_env=true. From now on, SDKMAN! will automatically switch the JDK as soon as you enter the project folder.

Removing Old Versions to Save Space

Each JDK version takes up about 500MB of disk space. When you no longer need one, you should remove it to clean up your machine:

sdk uninstall java 8.0.392-tem

This command wipes out all related files but absolutely does not affect other running Java versions.

Conclusion

Managing development environments shouldn’t be a barrier that slows down your progress. SDKMAN! makes Java installation as simple as using npm or pip. Real-world experience shows that this tool completely eliminates UnsupportedClassVersionError during deployment. Try implementing it today to optimize your workflow.

Share: