How To Install / Switch Between Multiple Java Versions Using SDKMAN
SDKMAN! is a tool to manage multiple versions of software development kits on macOS, Linux, WLS, Cygwin, Solaris and FreeBSD. It can install and manage Java, Groovy, Scala, Kotlin, Ceylon, Ant, Gradle, and many more.
The tool was inspired by RVM and rbenv, and it only requires curl and zip / unzip to be present on your system to work, being written in Bash (don't worry, it works with Zsh too).
This article shows how to install Java using SDKMAN! (using its installer, or offline using your own Java binaries), and how to manage and use multiple Java versions (easily setting a Java version as default, including setting the JAVA_HOME environment variable, or switching Java versions temporarily for the current terminal session, or only for a particular directory).
1. To get started, install the SDKMAN! command line interface.
2. List all the candidate Java versions.
Now that SDKMAN! is installed, let's put it to use. Use this command to list all the candidate Java versions:
sdk list java
This lists AdoptOpenJDK and the versions you can install, Amazon Java, Azul Zulu OpenJDK, Java.net etc.
This is an excerpt (the output is quite large) of what you'd see when using the "list java" command:
$ sdk list java
..............................................................
Available Java Versions
==============================================================
Vendor |Use| Version | Dist| Status | Identifier
--------------------------------------------------------------
AdoptOpenJDK| | 14.0.2.j9 | adpt| installed| 14.0.2.j9-adpt
| | 14.0.2.hs | adpt| | 14.0.2.hs-adpt
| | 13.0.2.j9 | adpt| | 13.0.2.j9-adpt
| | 13.0.2.hs | adpt| | 13.0.2.hs-adpt
| | 12.0.2.j9 | adpt| | 12.0.2.j9-adpt
| | 11.0.8.hs | adpt| installed| 11.0.8.hs-adpt
| | 8.0.262.j9| adpt| | 8.0.262.j9-adpt
..............................................................
Azul Zulu | | 14.0.2 | zulu| | 14.0.2-zulu
| | 13.0.4 | zulu| | 13.0.4-zulu
| | 13.0.3.fx | zulu| | 13.0.3.fx-zulu
| | 12.0.2 | zulu| | 12.0.2-zulu
| | 11.0.8 | zulu| | 11.0.8-zulu
..............................................................
Java.net | | 16.ea.6 | open| | 16.ea.6-open
| | 15.ea.32 | open| | 15.ea.32-open
| | 14.0.2 | open| | 14.0.2-open
| | 13.0.2 | open| | 13.0.2-open
| | 12.0.2 | open| | 12.0.2-open
| | 11.0.7 | open| | 11.0.7-open
| | 10.0.2 | open| | 10.0.2-open
| | 9.0.4 | open| | 9.0.4-open
| | 8.0.252 | open| | 8.0.252-open
.............................................................
Pay attention to the
Identifier
column. That's the package name that you need to use to install a Java version.3. Install a Java version using SDKMAN!.
The command that you can use to install a Java version is:
sdk install java IDENTIFIER
Replace
IDENTIFIER
with the Java version identifier, as seen in the sdk list java
command output.For example, to install AdoptOpenJDK version 11.0.8.hs, the install command is:
sdk install java 11.0.8.hs-adpt
SDKMAN! can also install a local version of Java, like this:
sdk install java [UNIQUE-IDENTIFIER] /path/to/Java
For example download the latest Oracle Java 14 .tar.gz (if you don't use our Oracle Java installer), extract it, then use SDKMAN! to install it using:
sdk install java 14.0.2-oracle ~/Downloads/jdk-14.0.2
4. Switch between Java versions.
There are 3 ways to manage Java versions using SDKMAN!:
- set a Java version as default
- use a Java version for the current terminal session only
- activate a particular Java version when you enter a directory
A. Set a Java version as default.
To set one of the Java versions installed using SDKMAN! (no matter if installed using its built-in installer or from a local directory) as the default Java version, use:
sdk default java IDENTIFIER
Replace
IDENTIFIER
with the Java version identifier, as seen in the sdk list java
command output.For example, to make AdoptOpenJDK version 11.0.8.hs the default system Java version, use:
sdk default java 11.0.8.hs-adpt
This also sets the JAVA_HOME environment variable to
~/.sdkman/candidates/java/current
.B. Use a Java version for the current terminal session only:
sdk use java IDENTIFIER
Replace
IDENTIFIER
with the Java version identifier, as seen in the sdk list java
command output.For example, set the locally installed 14.0.2-oracle Java as the Java version for the current terminal session:
sdk use java 14.0.2-oracle
C. Activate a particular Java version when you enter a directory.
Run this in the directory for which you want to use a custom Java version:
sdk env init
A file called
.sdkmanrc
has now been generated in this directory. Open it and change the value of java=
to the Java version identifier you want to use, e.g. 11.0.8.hs-adpt
for AdoptOpenJDK 11.0.8.Now either run
sdk env
in this directory to activate the Java version you've set in the .sdkmanrc
file, or edit the SDKMAN! configuration file (~/.sdkman/etc/config
) and set sdkman_auto_env=true
so that SDK versions are automatically switched when you cd
into a directory.No matter how you're using a Java version through SDKMAN!, you can check to see which Java version is currently in use (as set by SDKMAN) using:
sdk current java
You may also inquire Java directly about the current version in use:
java -version
javac -version