Skip to content

Ben Butterworth

Setting up Java on macOS

Software engineering, Java1 min read

Motivation

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.

jenv does 2 things

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").

Steps

  1. Make sure Homebrew is installed
  2. Read/ understand the AdoptOpenJDK homebrew tap
  3. Get the latest version: brew install --cask adoptopenjdk
  4. Add the tap so you can also get the other versions: brew tap AdoptOpenJDK/openjdk
  5. brew install --cask adoptopenjdk11 or with any other version you want.
  6. You can now either leave and read Jenv's getting started guide for more detail and up to date info, or read this which might be quicker.
  7. DO NOT modify JAVA_HOME yourself, because that is what 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.
  8. Install jenv: brew install jenv
  9. mkdir -p ~/.jenv/versions, to prepare the folders for jenv to work properly. This is a workaround for a bug.
  10. Try to run jenv and an error will show up. Read it and run the line of code it tells you to.
  11. jenv add /Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home, remember to modify the path for your specific version. Notice the Contents/Home subdirectory.
  12. Add more JDKs!
  • brew install --cask adoptopenjdk15
  • jenv add /Library/Java/JavaVirtualMachines/adoptopenjdk-15.jdk/Contents/Home/
  1. jenv versions to list all JDKs
  2. jenv 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
  3. java --version should show the version you just set.

Conclusion

After finding jenv, I know I won't be running into setting up Java anymore. I highly recommend it.

© 2021 by Ben Butterworth. All rights reserved.
theme by LekoArts