As a freelance developer and tech blogger I need to switch JDK's regularly. In the morning, I could be developing a legacy system and in the evening I'm writing an article on a JDK 14 preview feature such as text blocks. In today's post, I'll share some aliases that I use to easily switch JDK's on my MacBook's.
Short Introduction to 'java_home'
Luckily, macOS offers a handy tool to find information about installed JDK's, it's located in
/usr/libexec/java_home and it is very usefull in setting up a JDK or running against a specific JDK. The following fragment shows the
-V command which prints all the recognized JDK's:
When you pass a lowercase
v, you will get the path to the matching JDK:
We can also leverage the output, to update our
JAVA_HOME variable and by creating some aliasses you can easily switch, see the following aliases from my
For Java 8 and older the version should be prefixed with '1.'
When switching, we'll also show the output of
java -version to make it extra clear to what version we just switched. This leads us to the following output when switching:
Note on installing through Brew
When installing a specific JDK version via Brew it will/might not directly be visible for
java_home. After installing it will show a command you can execute to make sure it is registered properly, that will look something like the following:
java_home and some quick aliases we can easily use multiple JDK versions in parallel and switch without any issues.