Java is one of the most popular programming languages in use today. And a clean setup lets you seamlessly install Java and switch between different versions when you're building applications.

In this tutorial you will learn how to:

  • Install any Java version,
  • Switch between Java versions,
  • Update to the newest Java version.

The provided guide should work for most operating systems. I tested it for the following Linux versions:

  • Ubuntu
  • Debian
  • MacOS

Java Development Kit

The Java Development Kit (JDK) is a development environment for building applications, applets, and components using the Java programming language. (Source)

The JDK contains different applications, including

javac, the Java compiler, which converts source code into Java bytecode.

java, the loader for Java applications. This tool is an interpreter and can interpret the class files generated by the javac compiler.

Now a single launcher is used for both development and deployment. The old deployment launcher, jre, no longer comes with Sun JDK, and instead it has been replaced by this new java loader. (Source)

Java Build tools (Maven, Gradle, and so on) and your code editors use Java applications behind the scenes to give developers a nice experience running, creating, and maintaining applications.

Let’s see how to install Java in a Linux environment using the terminal. This enables you to use the steps in your own Linux environment and in many remote environments.

How to Use SDKMan to Manage Java Versions

SDKMAN! is a tool for managing parallel versions of multiple 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. (Source)

SDKMan comes with its own installer, which supports many operation systems. Make sure you install curl before, and execute the installer script after.

How to Install SDKMan on Ubuntu 22

# install curl
$ sudo apt install curl

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

How to Install SDKMan on Debian 11

# login as root
$ su

# install curl
$ apt install curl zip

# exit root user session
$ exit

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

How to Install SDKMan on MacOS

In case you don’t have brew and curl yet on Mac, you need to install them to easily install and update sdkman.

# install brew package manager)
$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

# install curl
$ brew install curl

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

Now, close and reopen your terminal to use sdkman.

# print sdkman version to verify installation
$ sdk versionSDKMAN 5.15.0

# install latest java
$ sdk install java

# check your java installation and print your java’s version
$ java –version

Openjdk version "17.0.3" 2022-04-19OpenJDK Runtime Environment Temurin-17.0.3+7 (build 17.0.3+7)OpenJDK 64-Bit Server VM Temurin-17.0.3+7 (build 17.0.3+7, mixed mode, sharing)

# Show path to current java version$ which java/home/sesigl/.sdkman/candidates/java/current/bin/java

Now, you are ready to use Java.

How to Install Multiple Java Versions

It is very useful to install multiple Java versions. Maybe certain applications require an older version of Java. Or you want to play with a brand new Java version and easily switch back.

Next, you additionally install Java 18:

$ sdk install java 18.0.1-tem
Done installing!

Do you want java 18.0.1-tem to be set as default? (Y/n): n

By typing n,  it means that you do not want to use Java 18 as your default version. You can manually enable versions temporarily in your shell by executing sdk use java <version>.

$ sdk use java 18.0.1-tem
Using java version 18.0.1-tem in this shell.

$ java -version
openjdk version "18.0.1" 2022-04-19OpenJDK Runtime Environment Temurin-18.0.1+10 (build 18.0.1+10)OpenJDK 64-Bit Server VM Temurin-18.0.1+10 (build 18.0.1+10, mixed mode, sharing)

If you close the window or type Java sdk use java 17.0.3-tem you can switch back.

$ sdk use java 17.0.3-tem
Using java version 17.0.3-tem in this shell.

$ java -version
openjdk version "17.0.3" 2022-04-19OpenJDK Runtime Environment Temurin-17.0.3+7 (build 17.0.3+7)OpenJDK 64-Bit Server VM Temurin-17.0.3+7 (build 17.0.3+7, mixed mode, sharing)

How to Automatically Switch the Java Version

Let’s say that you have 2 projects, one with Java 17 and one with Java 18. By creating a .sdkmanrc file in a directory, you can automatically switch versions, which will boost your productivity.

Let’s create a file for a Java 17 project:

$ sdk env init
.sdkmanrc created.

$ tail .sdkmanrc
# Enable auto-env through the sdkman_auto_env config
# Add key=value pairs of SDKs to use below
java=17.0.3-tem

Next, create another directory, switch the Java version to Java 18, and create another .sdkmanrc by executing sdk env init.

$ cd ..

$ mkdir my-java-18-project

$ cd my-java-18-project/

$ sdk use java 18.0.1-tem
Using java version 18.0.1-tem in this shell.

$ sdk env init
.sdkmanrc created.

$ tail .sdkmanrc
# Enable auto-env through the sdkman_auto_env config
# Add key=value pairs of SDKs to use below
java=18.0.1-tem

To automatically switch Java versions, you need to edit the file $HOME/.sdkman/etc/config and set sdkman_auto_env=true. There is already a line, so you only need to change false to true.

To enable the configuration change, restart your terminal. Once done, sdkman prints when it changes the Java version automatically for you.

Let’s verify the Java version as well.

$ cd my-java-17-project/
Using java version 17.0.3-tem in this shell.

$ java -version
openjdk version "17.0.3" 2022-04-19
OpenJDK Runtime Environment Temurin-17.0.3+7 (build 17.0.3+7)
OpenJDK 64-Bit Server VM Temurin-17.0.3+7 (build 17.0.3+7, mixed mode, sharing)

$ cd ..
Restored java version to 17.0.3-tem (default)

$ cd my-java-18-project/
Using java version 18.0.1-tem in this shell.

$ java -version
openjdk version "18.0.1" 2022-04-19
OpenJDK Runtime Environment Temurin-18.0.1+10 (build 18.0.1+10)
OpenJDK 64-Bit Server VM Temurin-18.0.1+10 (build 18.0.1+10, mixed mode, sharing)

If you want to know more about sdkman, check the sdkman usage documentation.

How to Update a Java Version

Once a new Java version is available, it should be listed via sdk list java. But, you can also use sdk upgrade java to ask sdkman to check for updates.

Let’s install an older version of Java:

$ sdk uninstall java 17.0.3-tem

$ sdk install java 17.0.2-tem

$ sdk install java 11.0.12-tem

$ sdk upgrade java
Available defaults:
java (local: 18.0.1-tem, 11.0.12-tem, 17.0.2-tem; default: 17.0.3-tem)

Use prescribed default version(s)? (Y/n): Y

Installing: java 17.0.3-tem
Done installing!
Setting java 17.0.3-tem as default.

By confirming with y, it downloads the suggested default version 17.0.3-tem and sets it as a default on your system. This makes future updates easy by executing sdk upgrade java.

Summary

In this article, you learned how manage Java SDKs easily using sdkman. It’s a very useful tool, which supports many Linux distributions including Ubuntu, Debian and MacOS.

SDKMan enables you to install and remove Java versions, switch between them, and upgrade your Java versions with a single command. This keeps your system clean and makes managing Java SDKs easy.

I hope you enjoyed the article.

If you liked it and felt the need to give me a round of applause 👏 or just want to get in touch 👋, follow me on Twitter. I work at eBay Kleinanzeigen, one of the world’s biggest classified companies. By the way, we are hiring!

References