How to set JAVA_HOME environment variable on Mac OS X 10.9?

I just spent 2 hours setting this variable. The other answers did not work properly for me. I'm using macOS Catalina 10.15.4.

First, find your actual Java SDK Home directory:

/usr/libexec/java_home

Manually navigate there to make sure you don't have any mistakes due to incorrect versions, etc. For me, this was:

/Library/Java/JavaVirtualMachines/jdk-13.0.2.jdk/Contents/Home

Next, edit your terminal's profile. If you're using zsh, this will be:

vim ~/.zshrc

If you're not using zsh, this will be:

vim ~/.bash_profile

Inside, add the following new line anywhere in the file:

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-13.0.2.jdk/Contents/Home

Restart your terminal app (or source ~/.bash_profile), and it should work properly.


I did it by putting

export JAVA_HOME=`/usr/libexec/java_home`

(backtics) in my .bashrc. See my comment on Adrian's answer.


If you're using bash, all you have to do is:

echo export "JAVA_HOME=\$(/usr/libexec/java_home)" >> ~/.bash_profile

If you're using zsh (which probably means you're running macOS Catalina or newer), then it should instead be:

echo export "JAVA_HOME=\$(/usr/libexec/java_home)" >> ~/.zshrc

In either case, restart your shell.

If you have multiple JDK versions installed and you want it to be a specific one, you can use the -v flag to java_home like so:

echo export "JAVA_HOME=\$(/usr/libexec/java_home -v 1.7)" >> ~/.bash_profile