Organizational Research By

Surprising Reserch Topic

where is java home on osx yosemite 10 10 mavericks 10 9 mountain lion 10

where is java home on osx yosemite 10 10 mavericks 10 9 mountain lion 10  using -'java,osx'

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.

asked Sep 24, 2015 by r3tt
0 votes

Related Hot Questions

15 Answers

0 votes

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.

answered Sep 24, 2015 by suyesh.lokhande
0 votes

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 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.

answered Sep 24, 2015 by param.oncemore
0 votes

/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: (article seems to have disappeared from the website, keeping the reference in place to at least give credit to the owner of the blog) 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.

answered Sep 24, 2015 by amit_cmps
0 votes

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.

answered Sep 24, 2015 by vimaldas2005
0 votes

None of these 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 follwoing two commands:

$ source ~/.bash_profile

$ echo $JAVA_HOME

to see output.

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

answered Sep 24, 2015 by shikhar jain
0 votes

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


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

answered Sep 24, 2015 by vickeykumar66
0 votes

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"
answered Sep 24, 2015 by shikhar jain
0 votes

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 /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home

answered Sep 24, 2015 by dhananjayksharma
0 votes

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

answered Sep 24, 2015 by manju bhargava
0 votes

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"

answered Sep 24, 2015 by vickeykumar66
0 votes

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 $@`
   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.

answered Sep 24, 2015 by abhi
0 votes

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/

answered Sep 24, 2015 by maurya
0 votes

For Mac Yosemite,

JDK 1.7.0_xx is using

$ ls -ltar /usr/bin/java


answered Sep 24, 2015 by ankitarajoria4
0 votes

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.

answered Sep 24, 2015 by gauravsinghal83
0 votes

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 :

answered Sep 24, 2015 by rajeevr642