Easy way to set your Java Home

Luiz Gustavo De O. Costa
4 min readFeb 13, 2022

For the nth time I had to change my Java version manually, using commands like /usr/libexec/java_home -V to list all vms and then export again, but once you close the terminal the path is lost. So, it’s time to use SDKMAN!

Doors choices choose decision

1. What is it?

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.

2. Why?

  • To avoid headache when you have to use a specific JVM version,
  • Fast JVM changing
  • Save your time and health, the more import why

3. How?

Using a few commands to install the SDKMAIN!

$ curl -s "https://get.sdkman.io" | bash
$ source "$HOME/.sdkman/bin/sdkman-init.sh"
$ sdk version

4. Hands on (TLDR)

4.1 Project based on JDK 17

  • Create a new class using a feature within JDK 17, for instance Records (is from JDK 16).
Main and Movie Java classes

Otherwise you can clone the project and open it in your IntelliJ.

IntelliJ — Java 17

When compiled we can see clearly the error, since the record is not recognized from Java 11, it is.

Java compiler error

4.2 How to solve it?

List all VMs, export the correct VM version to JAVA_HOME or use SDKMAN! In my case, let’s use SDKMAN!

Install the SDKMAN!

curl -s "https://get.sdkman.io" | bash
source "$HOME/.sdkman/bin/sdkman-init.sh"
sdk version

4.2.1. Next

Add JVM, for its use, list all VMs and then install what you want, in our case JVM 17.

sdk list java
sdk install java 17.0.2-open

Now, we can compile and run the project.

Java version, compile and execution

4.3. What if I need other version?

Easy, list all vms using sdkman list java pick your preferred version and use sdkman install java <your JDK version> see below for Coretto JDK 11

Install other JDK

Now, we have 2 JDKs, then how to change? First, list all you have using sdk list java

Result of sdk list java command

The column Status, show the installed value, ie, the versions installed in your machine. To change it, use the command sdk use java <your desired version>

Change the JDK version

5. Drawbacks

Until February 2022 I didn’t find an easy way to uninstall SDKMAN!.

6. Tech Stack

  • iTerm2
  • SDKMAN!
  • openJDK 17

7. References

https://pixabay.com/photos/doors-choices-choose-decision-1690423/

https://www.baeldung.com/java-sdkman-intro

--

--

Luiz Gustavo De O. Costa

Hey friend!! I’m Luiz Gustavo, a Java developer and I’m here to learn and write about Java, tests and good practices