Home » Java » Where is JAVA_HOME on OSX Sierra (10.12), El Capitan (10.11), Yosemite (10.10), Mavericks (10.9), Mountain Lion (10.8) or OSX Lion (10.7)?

Where is JAVA_HOME on OSX Sierra (10.12), El Capitan (10.11), Yosemite (10.10), Mavericks (10.9), Mountain Lion (10.8) or OSX Lion (10.7)?

Posted by: admin November 2, 2017 Leave a comment


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

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


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

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.


None of the above answers helped me. I suppose all the answers are for older OS X

For OS X Yosemite 10.10, follow these steps

Use your favorite text editor to open: ~/.bash_profile

//This command will open the file using vim
$ vim ~/.bash_profile

Add the following line in the file and save it ( : followed by “x” for vim):

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

Then in the terminal type the following two commands to see output:

$ source ~/.bash_profile

$ echo $JAVA_HOME

In the second line, you are updating the contents of .bash_profile file.


The above didn’t work for me with Amazon’s EC2 tools, because it expects bin/java etc. underneath JAVA_HOME. /System/Library/Frameworks/JavaVM.framework/Home did work.


For OS X you can do:

export JAVA_HOME=`/System/Library/Frameworks/JavaVM.framework/Versions/Current/commands/java_home`


On Mac OS X Lion, to set visualgc to run, I used:

export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home


The following worked for me. I’m using ZSH on OSX Yosemite with Java 8 installed.

The following command /usr/libexec/java_home emits the path to JDK home:


In your ~/.zshrc,

export JAVA_HOME = "/Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home"


A better (more upgradable) way is to use the following:


This should work with AWS also since it has bin underneath Home


Newer Oracle JVMs such as 1.7.0_21-b12 seem to install here:

/Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home


My approach is:


export JAVA6_HOME=`/usr/libexec/java_home -v 1.6`
export JAVA7_HOME=`/usr/libexec/java_home -v 1.7`

# -- optional
# export PATH=$JAVA_HOME/bin:$PATH

This makes it very easy to switch between J6 and J7


For Mac OS X 10.9 I installed the latest version of JRE from Oracle and then reset the JAVA_HOME to /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home.

I am sure there is a better way but got me up and running.

hughsmac:~ hbrien$ echo $JAVA_HOME


OSX Yosemite, ZSH, and Java SE Runtime Environment 8, I had to:

$ sudo ln -s /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands /System/Library/Frameworks/JavaVM.framework/Versions/Current/bin

and in ~/.zshrc change JAVA_HOME to
export JAVA_HOME="/System/Library/Frameworks/JavaVM.framework/Versions/Current"


Update for Java 9 and some neat alias.

In .bash_profile:

export JAVA_HOME8=`/usr/libexec/java_home --version 1.8`
export JAVA_HOME9=`/usr/libexec/java_home --version 9`

Note, that for the latest version it is 9 and not 1.9.

Set active Java:

export PATH=$JAVA_HOME/bin:$PATH

Some additional alias to switch between the different versions:

alias j8='export JAVA_HOME=$JAVA_HOME8; export PATH=$JAVA_HOME/bin:$PATH'
alias j9='export JAVA_HOME=$JAVA_HOME9; export PATH=$JAVA_HOME/bin:$PATH'

Test in terminal:

% j8
% java -version
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)
% j9
% java -version
java version "9"
Java(TM) SE Runtime Environment (build 9+181)
Java HotSpot(TM) 64-Bit Server VM (build 9+181, mixed mode)


Got the same issue after I upgrade my Mac OS and following worked for me:

  1. cmd>vi ~/.bash_profile

  2. Add/update the line for JAVA_HOME:
    export JAVA_HOME=”/Library/Java/JavaVirtualMachines/jdk1.7.0_15.jdk/Contents/Home”

  3. cmd>source ~/.bash_profile or open a new terminal

I think the jdk version might differ, so just use the version which you have under /Library/Java/JavaVirtualMachines/


If you are in need to have multiple versions of JDK under Mac OS X (Yosemite), it might be helpful to add some scripting for automated switching between them.

What you do is to edit your ~/.bash_profile and add the following:

function setjdk() {
  if [ $# -ne 0 ]; then
   removeFromPath '/System/Library/Frameworks/JavaVM.framework/Home/bin'
   if [ -n "${JAVA_HOME+x}" ]; then
    removeFromPath $JAVA_HOME
   export JAVA_HOME=`/usr/libexec/java_home -v [email protected]`
   export PATH=$JAVA_HOME/bin:$PATH
 function removeFromPath() {
  export PATH=$(echo $PATH | sed -E -e "s;:$1;;" -e "s;$1:?;;")
setjdk 1.7

What the script does is to first remove other JDK versions in the PATH so that they won’t interfere with our new JDK version. Then it makes some clever use of /usr/libexec/java_home which is a command that lists installed JDK versions. The -v argument tells java_home to return the path of the JDK with the supplied version, for example 1.7. We also update the PATH to point to the bin directory of the newly found JAVA_HOME directory. At the end we can simply execute the function using

setjdk 1.7

which selects the latest installed JDK version of the 1.7 branch. To select a specific version you can simply execute

setjdk 1.7.0_51

instead. Run /usr/libexec/java_home -V to get more details on how to choose versions.

P.S. Do not forget to source ~/.bash_profile after you save it.


For Mac Yosemite,

JDK 1.7.0_xx is using

$ ls -ltar /usr/bin/java




Anyone using AUSKEY from the Australian Tax Office (ATO) should uninstall AUSKEY.
This sorted out my JAVA_HOME issues.

It is also no longer required for MAC users. Yah!


This answer is related to Mountain Lion and not Lion. I needed to do this for the AWS Command Line Tools. According to the AWS docs, running which java returns /usr/bin/java.

So, I set JAVA_HOME=/usr in my .bashrc.

Apparently, /usr/bin/java is a symbolic link to /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java which makes it all work.


As mentioned in the comment below, this JAVA_HOME value is not an ideal solution when the JAVA_HOME environment variable is to be used by things other than the AWS Command Line Tools. It works fine for the AWS Command Line Tools, though, as given in their docs.


for mac user .
java 8 should add

export JAVA_HOME=`/usr/libexec/java_home -v 1.8`
# JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home

java 6 :

export JAVA_HOME=`/usr/libexec/java_home -v 1.6`
# JAVA_HOME=/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

ref :http://qiita.com/seri_k/items/e978c1339ce51f13e297