Java – Where is JAVA_HOME on macOS Mojave (10.14) to Lion (10.7)

javamacos

Java is an optional package on the latest versions of macOS.

Yet once installed it appears like the JAVA_HOME environment variable is not set properly.

Best Answer

With the Java optional package or Oracle JDK installed, adding one of the following lines to your ~/.bash_profile file will set the environment variable accordingly.

export JAVA_HOME="$(/usr/libexec/java_home -v 1.6)"
or
export JAVA_HOME="$(/usr/libexec/java_home -v 1.7)"
or
export JAVA_HOME="$(/usr/libexec/java_home -v 1.8)"
or simply
export JAVA_HOME="$(/usr/libexec/java_home)"

Note: If you installed openjdk on mac using brew, run sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk for the above to work

Update: added -v flag based on Jilles van Gurp response.