Skip to content

Instantly share code, notes, and snippets.

@levibostian
Last active February 19, 2021 22:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save levibostian/3c70bd7e78d76454c165a8f32f1ff6e9 to your computer and use it in GitHub Desktop.
Save levibostian/3c70bd7e78d76454c165a8f32f1ff6e9 to your computer and use it in GitHub Desktop.
Installing and managing Java on macOS. Handy for Android dev.

Android Studio comes with a SDK for java built in. You can use that as it defaults pretty easily. However, you may need more.

Installing java

We love homebrew. That is our preferred way of installing everything, including java. The instructions below are up to date as of the time of writing, but it may not work anymore. Check out this answer to get the latest tips on how to install java.

brew tap AdoptOpenJDK/openjdk
brew install --cask adoptopenjdk # this installs the latest version of java. 

# use `brew search adoptopenjdk` to find all of the versions of java you can install. 

To switch between different versions of java, there are many methods. We will use a simple, manual method outlined here and also inspired here.

Add this to your .bashrc:

export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)
export JAVA_9_HOME=$(/usr/libexec/java_home -v9)
export JAVA_10_HOME=$(/usr/libexec/java_home -v10)
export JAVA_11_HOME=$(/usr/libexec/java_home -v11)
export JAVA_12_HOME=$(/usr/libexec/java_home -v12)
export JAVA_13_HOME=$(/usr/libexec/java_home -v13)
export JAVA_14_HOME=$(/usr/libexec/java_home -v14)
export JAVA_15_HOME=$(/usr/libexec/java_home -v15)

alias java8='export JAVA_HOME=$JAVA_8_HOME'
alias java9='export JAVA_HOME=$JAVA_9_HOME'
alias java10='export JAVA_HOME=$JAVA_10_HOME'
alias java11='export JAVA_HOME=$JAVA_11_HOME'
alias java12='export JAVA_HOME=$JAVA_12_HOME'
alias java13='export JAVA_HOME=$JAVA_13_HOME'
alias java14='export JAVA_HOME=$JAVA_14_HOME'
alias java15='export JAVA_HOME=$JAVA_15_HOME'

# default to Java 15
java15

Then, you can switch by just typing in your terminal java13.

You can use jenv if you want but that is now overkill with this simple set of alias commands.

jenv

(this is here for archival purposes. It's preferred to no longer use it but the simple aliases)

You may now have multiple versions of java installed. You may have them all installed by homebrew or installed otherwise. Brew is recommended because it's so easy but you don't need to.

But then you need to be able to tell your OS or your project what version of java to use! There is a handy tool, jenv, that allows you to do that.

brew install jenv

After jenv installs, it may require that you then add something to your .bashrc file. As of today, this is what it tells you to add:

export PATH="$HOME/.jenv/bin:$PATH"
eval "$(jenv init -)"

Read this doc on installing jenv as there may be more directions that are different then this time.

Now that we have installed jenv, we need to tell jenv where our java installations are. jenv will not install java for you, just point the OS or project to it.

Run /usr/libexec/java_home to tell you where Java is installed on your machine. It will probably be something like this: /Library/Java/JavaVirtualMachines/openjdk-14.0.2.jdk/Contents/Home. If you run ls /Library/Java/JavaVirtualMachines/, you may find multiple entries.

Now, we can do this:

jenv add /Library/Java/JavaVirtualMachines/openjdk-14.0.2.jdk/Contents/Home

jenv should tell you that it successfully added that version. Repeat this step for all of your java versions.

Then, run jenv global 11 where you replace 11 with what you choose to be the OS default java version to run.

You can now run jenv doctor to make sure that everything is running as expected and echo $JAVA_HOME to make sure that it's pointing to a .jenv directory.

Note: you can also set a local java version for a specific project. This allows you to tell jenv what version of java to use for each project instead of the OS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment