591

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.

3
  • 1
    Possible duplicate of What should I set JAVA_HOME to on OSX
    – mipadi
    Commented Nov 11, 2016 at 18:12
  • On Mac Sierra I've /Library/Java/JavaVirtualMachines which has jdk and System Preferences > Java what's different between these 2 ?
    – vikramvi
    Commented Jul 25, 2017 at 7:57
  • Don't underestimate the chances that your Java installation may not have completed properly. If none of the below excellent suggestions seem to work, try [re]installing!
    – MarkHu
    Commented Sep 13, 2017 at 21:32

27 Answers 27

1098

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.

16
  • 1
    Good to know. I have to say im not too thrilled with the feature list for Lion. There doesnt seem to much there in the way of enhancements for my usage... Commented Jul 6, 2011 at 2:29
  • 11
    this didn't work for me. /System/Library/Frameworks/JavaVM.framework/Home worked. Commented Apr 25, 2012 at 4:35
  • 7
    Works on Maverick, too!
    – cbare
    Commented Nov 15, 2013 at 21:49
  • 2
    how come /usr/libexec/java_home -V doesn't show me java 1.8 that I just installed and is available on /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin
    – mwm
    Commented Oct 17, 2014 at 10:59
  • 3
    For Java 11 and High Sierra, export JAVA_HOME="$(/usr/libexec/java_home -v 11)"
    – Willie Z
    Commented Oct 10, 2018 at 7:13
125

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

2
84

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.

4
  • 1
    It would have worked if you had installed java 6 from Apple. the point here is that you are using oracle java 7. Commented Dec 30, 2012 at 5:58
  • 2
    This works for me on Mavricks very well but the location of the java_home script is different. export JAVA_HOME="$(/System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java_home)" Commented Oct 24, 2013 at 10:38
  • 1
    I tried to set up maven on mac Yosemite, this way to set JAVA_HOME works for me.
    – Alocus
    Commented Mar 21, 2015 at 22:41
  • Works for me on Sierra 10.12 and Java 8
    – Kanak Sony
    Commented Aug 21, 2017 at 9:18
44

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.

6
  • I think I got your pb - you use quotes instead of backquotes ' vs ` Commented Jun 16, 2015 at 14:19
  • No i'm using the right quotes, instead I tried copy pasting, it didn't work. I updated my answer because it worked for me.
    – Giri
    Commented Jun 16, 2015 at 14:25
  • @OlivierRefalo I can see you've updated your answer with double quotes("). But even without quotes, it works.
    – Giri
    Commented Jun 16, 2015 at 14:27
  • Thanks a lot . It worked for me to in Yosemite 10.10
    – Vijay
    Commented Aug 8, 2015 at 18:08
  • 2
    This is the only solution, that really works. Adding path by any other solution, will add on temporary basis, until you terminate the terminal, and the path vanishes. To be added permanently, the path must be saved in .bash_profile file. Meanwhile, I used nano rather than vim, for the simplicity. Commented Mar 11, 2016 at 8:02
36

Update for Java 9 and some neat aliases.

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 JAVA_HOME=$JAVA_HOME8
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)

EDIT: Update for Java 10

export JAVA_HOME10=`/usr/libexec/java_home --version 10`
alias j10='export JAVA_HOME=$JAVA_HOME10; export PATH=$JAVA_HOME/bin:$PATH'

EDIT: Update for Java 11

export JAVA_HOME11=`/usr/libexec/java_home --version 11`
alias j11='export JAVA_HOME=$JAVA_HOME11; export PATH=$JAVA_HOME/bin:$PATH'
0
30

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.

4
  • Was it lion ? did you get the official Java package from Apple, or Java6/7 from Oracle ? Commented Jan 27, 2012 at 16:13
  • I believe it was Apple's Java package.
    – user92923
    Commented Feb 2, 2012 at 18:55
  • 1
    @AndrewH yes I found the same to be true. Installed Apple's Java package and discovered the actual library path (for example, to set -vm in eclipse.ini): /System/Library/Java/Support/Deploy.bundle/Contents/Home/lib/javaws.jar
    – noogrub
    Commented Jul 4, 2012 at 15:30
  • Thanks for the info. Maybe I'm too simple, but why does OS X make such a simple thing a complete mess?
    – Kuberchaun
    Commented Mar 16, 2015 at 14:58
19

For OS X you can do:

export JAVA_HOME=`/System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java_home`
1
  • Of note, HFS+ is case insensitive.
    – sufinawaz
    Commented Oct 19, 2014 at 16:05
15

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
2
  • 1
    I don't get your proposal: how is that different from export JAVA_HOME=/usr/libexec/java_home ? the command /usr/libexec/java_home returns /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home Commented Jan 27, 2012 at 16:12
  • For visualgc /usr/libexec/java_home yields: The java.exe found at: /usr/libexec/java_home is not in a JDK directory. Please set and export your JVMSTAT_JAVA_HOME environment variable to refer to a directory containing the Sun J2SE 1.5.0 JDK (not a JRE) and try again.
    – EGHM
    Commented Feb 2, 2012 at 0:29
15

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:

/Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home

In your ~/.zshrc,

export JAVA_HOME = "/Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home"
4
  • How is that different that the proposed solution? Commented Oct 28, 2014 at 13:26
  • It's not different to the proposed solution. This solution is specific to people using ZSH, Java 8 and OSX Yosemite.
    – Hemanta
    Commented Oct 28, 2014 at 21:45
  • Confirmed working like a charm with java 1.8 installed on OSX Yosemite, cheers mate.
    – Ashoor
    Commented Jan 19, 2015 at 2:22
  • The general solution works just fine for Java 8, zsh, and Yosemite. Commented Jul 23, 2017 at 16:27
13

for macOS Mojave 10.14.1 and JAVA 11.0.1 I set the profile as

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

key in terminal this to confirm:

$JAVA_HOME/bin/java -version

java version "11.0.1" 2018-10-16 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.1+13-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.1+13-LTS, mixed mode)
1
  • 1
    Works on Catalina. Ty Commented Nov 7, 2019 at 15:17
11

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

/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home

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

1
  • 1
    kind of disagree with that statement "more upgradable" /usr/libexec/java_home is an exe that returns a path,not a fixed path. Besides depending on what jvm version you use, the path above won't match Commented Oct 11, 2012 at 16:55
11

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

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

3
  • 1
    Indeed - and the /usr/libexec/java_home tool returns an error for these Java versions. Commented Aug 8, 2013 at 14:08
  • 1
    This is THE only answer working for me after downloading v1.7 JRE Commented Sep 5, 2014 at 8:08
  • 3
    This is the path if you install the JRE. Most of the rated answers and comments are about the JDK. Not the same thing.
    – 8None1
    Commented Nov 20, 2014 at 18:09
6

My approach is:

.bashrc

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

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

This makes it very easy to switch between J6 and J7

0
5

I Had to explicitly set it to the exact path on my Macbook air .

Steps followed:

  1. try to echo $JAVA_HOME (if it's set it'll show the path), if not, try to search for it using sudo find /usr/ -name *jdk
  2. Edit the Bash p with - sudo nano ~/.bash_profile
  3. Add the exact path to JAVA Home (with the path from step 2 above) export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home
  4. Save and exit
  5. Check JAVA_Home using - echo $JAVA_HOME

I am running MACOS MOJAVE - 10.14.2 (18C54) on a Macbook Air with JAVA 8

3

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"

3

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 this got me up and running.

hughsmac:~ hbrien$ echo $JAVA_HOME /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home

1
  • 2
    On a fresh Mavericks install, this is the only solution that worked for me. Had to point sqldeveloper to the jdk home located here (as suggested by this thread to get it to work.
    – chris
    Commented Nov 25, 2013 at 18:05
2

For Java 11 (JDK 11) it can be located with the following command:

/usr/libexec/java_home -v 11
1

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/

1

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
   fi
   export JAVA_HOME=`/usr/libexec/java_home -v $@`
   export PATH=$JAVA_HOME/bin:$PATH
  fi
 }
 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.

1

For Fish terminal users on Mac (I believe it's available on Linux as well), this should work:

set -Ux JAVA_8 (/usr/libexec/java_home --version 1.8)
set -Ux JAVA_12 (/usr/libexec/java_home --version 12)
set -Ux JAVA_HOME $JAVA_8       //or whichever version you want as default
1

Handy command for knowing java home and other details.

java -XshowSettings:properties -version

and to know all java homes on your mac:

/usr/libexec/java_home -V
1
  • I recently figured this one out myself and came looking here to share to find you already discovered; had posted on Ask Ubuntu This is a good solution if java is on the PATH but when some tool/application actually needs JAVA_HOME set. Commented Jul 25, 2023 at 8:00
0

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.

Update

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.

1
  • the point is, /usr is not a correct JAVA_HOME directory. It's ok to locate bin/java. But sometimes builders (ant, make) use JAVA_HOME to reference the javac compiler in lib/tools.jar Your solution will fail for those Commented Oct 29, 2012 at 2:35
0

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

0

For Mac Yosemite,

JDK 1.7.0_xx is using

$ ls -ltar /usr/bin/java
/System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java

JAVA_HOME

/Library/Java/JavaVirtualMachines/jdk1.7.0_xx.jdk/Contents/Home
1
  • Can you please add more information to make this answer useful.
    – Mika
    Commented Jun 16, 2015 at 16:14
0

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!

0

I'm using Fish shell on High Sierra 10.13.4 and installed Java via Brew.

It's not automatically set up so to set it correctly on my system I run:

set -U JAVA_HOME (/usr/libexec/java_home)
0

Just set java_home of 1.8 jdk version in netbeans.conf file:

/Applications/NetBeans/NetBeans 8.2.app/Contents/Resources/NetBeans/etc/netbeans.conf

uncomment line:

netbeans_jdkhome="path/to/jdk"

and set path to your 1.8 jdk, in my case:

netbeans_jdkhome="/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home"

This approach lays you to have several jdk versions on mac os

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.