— Software engineering, Java — 1 min read
Like normal people, I duck'd for a way to setup Java JDK on my machine, because I forgot. This time, I noticed MKYong's method just uses brew to install java JDK versions (recommended by OpenJDK) but then sets JAVA_HOME explicitly. He doesn't use jenv
, citing simplicity and no black box magic. MKYong modifies JAVA_HOME
to change the java version, and this will hide all other JAVA versions from being listed in /usr/libexec/java_home -V
. Also, if that path is broken, no versions show up. I suggest not modifying JAVA_HOME, because in a few months when you want to switch your java version, java_home
will still show you which versions you have on your machine.
It allows switching between Java versions and sets JAVA_HOME for you. You can just read jenv
's Getting Started Guide now, but I've written the rest for my convenience, since I've set up too many machines this year (e.g. M1 Mac Mini, Intel i7 Hackintosh, MacBook Pro 16" from Popsa and my personal Intel MacBook Pro 13").
brew install --cask adoptopenjdk
brew tap AdoptOpenJDK/openjdk
brew install --cask adoptopenjdk11
or with any other version you want.jenv
will do. If you're updating an older system, get rid of your old code, e.g. export JAVA_HOME=...
from your .bash_profile
, .zprofile
, .zshrc
, or wherever you might have put it.jenv
: brew install jenv
mkdir -p ~/.jenv/versions
, to prepare the folders for jenv to work properly. This is a workaround for a bug.jenv
and an error will show up. Read it and run the line of code it tells you to.jenv add /Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home
, remember to modify the path for your specific version. Notice the Contents/Home
subdirectory.brew install --cask adoptopenjdk15
jenv add /Library/Java/JavaVirtualMachines/adoptopenjdk-15.jdk/Contents/Home/
jenv versions
to list all JDKsjenv global openjdk64-11.0.9.1
to set a specific JDK, which also sets the JAVA_HOME variable for you. Check it with echo $JAVA_HOME
java --version
should show the version you just set.After finding jenv
, I know I won't be running into setting up Java anymore. I highly recommend it.