Tags: java
As a Java developer, you often need to switch between multiple versions of Java for different projects. Managing this manually can be tedious, but SDKMAN! is a great tool that simplifies the process of installing, listing, and switching between Java versions.
In this guide, we'll explore how to use SDKMAN! to:
List available and installed Java versions.
Install specific Java versions.
Switch between Java versions.
Set default and local versions for your projects.
First, if you don't have SDKMAN! installed, you can install it by running the following commands in your terminal:
curl -s "https://get.sdkman.io" | bash
source "$HOME/.sdkman/bin/sdkman-init.sh"
sdk version
Once SDKMAN! is installed, you can list all available Java versions and the ones you have installed locally.
sdk list java
sdk current java
This will show you the Java versions you currently have installed on your machine.
sdk install java 17.0.0-open
This command downloads and installs OpenJDK 17. SDKMAN! will set this version as the default unless you specify otherwise.
You may need to switch Java versions frequently, depending on your project. SDKMAN! makes this seamless.
To switch the default Java version globally (for all projects), use the sdk use command. For example:
sdk use java 11.0.0-open
This sets the default Java version system-wide to OpenJDK 11.
If you want to set a Java version for a specific project, navigate to your project folder and run:
sdk env init
This creates a .sdkmanrc file where you can specify the Java version. To apply the version defined in .sdkmanrc, simply run:
sdk env
SDKMAN! will automatically switch to the Java version specified in the project directory.
To set a default version that persists across terminal sessions:
sdk default java 17.0.0-open
This command sets OpenJDK 17 as the default version system-wide.