Where is JAVA_HOME on macOS Mojave (10.14) to Lion (10.7)? Where is JAVA_HOME on macOS Mojave (10.14) to Lion (10.7)? java java

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


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)"orexport JAVA_HOME="$(/usr/libexec/java_home -v 1.7)"orexport JAVA_HOME="$(/usr/libexec/java_home -v 1.8)"or simplyexport 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.


/usr/libexec/java_home is not a directory but an executable. It outputs the currently configured JAVA_HOME and doesn't actually change it. That's what the Java Preferences app is for, which in my case seems broken and doesn't actually change the JVM correctly. It does list the 1.7 JVM but I can toggle/untoggle & drag and drop all I want there without actually changing the output of /usr/libexec/java_home.

Even after installing 1.7.0 u6 from Oracle on Lion and setting it as the default in the preferences, it still returned the apple 1.6 java home. The only fix that actually works for me is setting JAVA_HOME manually:

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_06.jdk/Contents/Home/

At least this way when run from the command line it will use 1.7. /usr/libexec/java_home still insists on 1.6.

Update: Understanding Java From Command Line on OSX has a better explanation on how this works.

export JAVA_HOME=`/usr/libexec/java_home -v 1.7` 

is the way to do it. Note, updating this to 1.8 works just fine.


For me, Mountain Lion 10.8.2, the solution most voted does not work.I installed jdk 1.7 via Oracle and maven from homebrew.

My solution is from the hadoop-env.sh file of hadoop which I installed from homebrew, too.I add the below sentence in ~/.bash_profile, and it works.

export JAVA_HOME="$(/usr/libexec/java_home)"

This solution also works for OS X Yosemite with Java 1.8 installed from Oracle.