Easy way to set your Java Home
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!
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).
Otherwise you can clone the project and open it in your IntelliJ.
When compiled we can see clearly the error, since the record is not recognized from Java 11, it is.
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.
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
Now, we have 2 JDKs, then how to change? First, list all you have using sdk list java
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>
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/