Core Java

Manage multiple Java SDKs with asdf with ease

asdf is a helpful command-line tool that allows you to manage and switch between different versions of programming language runtimes including Java, Kotlin, Node.js, Python and their associated packages and libraries including Gradle, Maven, Yarn, Spring Boot CLI.

asdf is supported for all major operating systems including Windows via Windows Subsystem for Linux 2 (WSL2).

Depending on your setup (OS, shell and installation method) you need to perform several steps in order to make sure asdf works correctly. Consult the documentation for more details: https://asdf-vm.com/guide/getting-started.html.

I work on macOS with Iterm2 and oh-my-zsh, so the setup was to install asdf with Homebrew (brew install asdf) and then to install asdf for oh-my-zsh (https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/asdf)

Install and manage Java versions

Once you have asdf installed, you can use the following steps to install and manage Java versions:

  • Add the Java plugin to asdf by running: asdf plugin add java.
  • List all available Java versions by running: asdf list all java. Tip: Pipe the command with grep if you are looking for specific runtime.
  • Use the asdf install command to install a specific Java version. For example, to install GraalVM with Java 19 support, you would run asdf install java graalvm-22.3.0+java19:
➜ ~ asdf install java graalvm-22.3.0+java19

###################### 100.0%

graalvm-ce-java19-darwin-amd64-22.3.0.tar.gz

graalvm-ce-java19-darwin-amd64-22.3.0.tar.gz: OK
  • Use the asdf global command to set the default version of Java to use on your machine. For example, to set previously installed version as the default version, you would run asdf global graalvm-22.3.0+java19:
➜ ~ asdf global java graalvm-22.3.0+java19

➜ ~ java -version

openjdk version "19.0.1" 2022-10-18

OpenJDK Runtime Environment GraalVM CE 22.3.0 (build 19.0.1+10-jvmci-22.3-b08)

OpenJDK 64-Bit Server VM GraalVM CE 22.3.0 (build 19.0.1+10-jvmci-22.3-b08, mixed mode, sharing)

Global defaults are managed in $HOME/.tool-versions file:

➜ ~ tail $HOME/.tool-versions

java graalvm-22.3.0+java19

In contrary, when you want to use version specific to a directory you would use asdf local command which creates $PWD/.tool-versions file. Now, each you navigate to that directory, asdf picks the versions and set them properly for this directory only.

Install other tools

There are many language runtimes and tools you can manage with asdf. The process is as follows:

  • You look for the plugin (e.g. asdf plugin list all | grep gradle)
  • You add the plugin (e.g. asdf plugin add gradle)
  • You find the version you want to manage (e.g. asdf list all gradle)
  • You install the selected version of langugate runtime or tool (e.g. asdf install gradle 7.6)

Alternative: SDKMAN!

Before I discovered asdf, I used SDKMAN!. SDKMAN! is a tool for managing parallel versions of multiple SDKs and once installed, it provides a convenient sdk command for installing, switching, removing and listing installed SDKs. Learn more about it from my blog post: Manage multiple Java SDKs with SDKMAN! with ease

Published on Java Code Geeks with permission by Rafal Borowiec, partner at our JCG program. See the original article here: Manage multiple Java SDKs with asdf with ease

Opinions expressed by Java Code Geeks contributors are their own.

Rafal Borowiec

Software developer, Team Leader, Agile practitioner, occasional blogger, lecturer. Open Source enthusiast, quality oriented and open-minded.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button