587

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.

Olivier Refalo
  • 50,287
  • 22
  • 91
  • 122
  • 1
    Possible duplicate of [What should I set JAVA\_HOME to on OSX](http://stackoverflow.com/questions/1348842/what-should-i-set-java-home-to-on-osx) – mipadi Nov 11 '16 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 Jul 25 '17 at 07: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 Sep 13 '17 at 21:32

27 Answers27

1094

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.

Olivier Refalo
  • 50,287
  • 22
  • 91
  • 122
  • Does this only take care or the CLI? I know in 10.5 and below if you need to set a custom location (outside of the normal structure) you often need to to edit the ~/.MacOSX for GUI launched apps. – prodigitalson Jul 05 '11 at 20:25
  • 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... – prodigitalson Jul 06 '11 at 02:29
  • 1
    @Oliver: ewww :-) Thats probably the least useful thing... i always have multiple windows opened and arranged on screen so i can see multiple things at once... usually a different layout on every space. – prodigitalson Jul 06 '11 at 15:38
  • 11
    this didn't work for me. /System/Library/Frameworks/JavaVM.framework/Home worked. – chaostheory Apr 25 '12 at 04:35
  • 1
    The backticks were my undoing. This worked after copying and pasting properly :) – loeschg Jul 17 '13 at 18:45
  • 7
    Works on Maverick, too! – cbare Nov 15 '13 at 21:49
  • For me @chaostheory's solution worked. On OSX Mavericks. – Dries Vints Jan 27 '14 at 10:30
  • 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 Oct 17 '14 at 10:59
  • @chaostheory I tried this but it says "... is a directory" – clintgh Nov 05 '14 at 03:47
  • It's an issue actually, we shouldn't be forced to change our dotfiles in Yosemite to use Java JRE. Installing JDK solves the problem (no changes to .*profile files required) however again, we shouldnt be forced to install ~500MB JDK just to run stuff that requires much smaller JRE (like SquirrelSQL). – januszm Nov 27 '14 at 15:43
  • Works on Yosemite. Be careful with the ticking as what @loeschg mentioned, copy & paste does the trick. – Roy Lee Jun 04 '15 at 16:05
  • Worked for me. I had to restart Terminal before the difference took effect. – Tom Johnson Sep 29 '15 at 20:55
  • also found this link which may help as well: https://gist.github.com/johan/10590467 – AKFourSeven Jan 24 '17 at 10:24
  • Works on Mac OS X High Sierra 10.13.6 – Jason Rice Sep 05 '18 at 20:57
  • 3
    For Java 11 and High Sierra, export JAVA_HOME="$(/usr/libexec/java_home -v 11)" – Willie Z Oct 10 '18 at 07:13
  • The double quotes are not required – firstpostcommenter Jul 04 '19 at 15:36
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.

klanomath
  • 107
  • 1
  • 7
Jilles van Gurp
  • 7,927
  • 4
  • 38
  • 46
  • blog.hgomez.net/2012/07 is broken _but_ it's fixed in the answer which now links to http://web.archive.org/web/20140813164713/http://blog.hgomez.net/blog/2012/07/20/understanding-java-from-command-line-on-osx/ – Matt C Dec 30 '16 at 10:57
  • export JAVA_HOME="/usr/libexec/java_home -v 1.8" – Leo Nguyen Aug 01 '18 at 04:45
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.

Per Quested Aronsson
  • 11,380
  • 8
  • 54
  • 76
onpduo
  • 979
  • 6
  • 3
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.

Giri
  • 2,704
  • 2
  • 25
  • 27
  • I think I got your pb - you use quotes instead of backquotes ' vs ` – Olivier Refalo Jun 16 '15 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 Jun 16 '15 at 14:25
  • @OlivierRefalo I can see you've updated your answer with double quotes("). But even without quotes, it works. – Giri Jun 16 '15 at 14:27
  • Thanks a lot . It worked for me to in Yosemite 10.10 – Vijay Aug 08 '15 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. – Nadeem Jamali Mar 11 '16 at 08:02
  • export JAVA_HOME=$(/usr/libexec/java_home) echo $JAVA_HOME /Library/Java/JavaVirtualMachines/jdk-10.0.2.jdk/Contents/Home cordova requirements Java JDK: not installed – Alexey Sh. Dec 21 '18 at 17:09
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'
Peter Keller
  • 7,526
  • 2
  • 26
  • 29
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.

  • Was it lion ? did you get the official Java package from Apple, or Java6/7 from Oracle ? – Olivier Refalo Jan 27 '12 at 16:13
  • I believe it was Apple's Java package. –  Feb 02 '12 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 Jul 04 '12 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 Mar 16 '15 at 14:58
20

For OS X you can do:

export JAVA_HOME=`/System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java_home`
Community
  • 1
  • 1
sufinawaz
  • 3,623
  • 1
  • 25
  • 24
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"
Hemanta
  • 544
  • 6
  • 8
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
apaderno
  • 28,547
  • 16
  • 75
  • 90
EGHM
  • 2,144
  • 23
  • 37
  • 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 – Olivier Refalo Jan 27 '12 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 Feb 02 '12 at 00:29
12

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)
Jiulong Zhao
  • 1,313
  • 1
  • 15
  • 25
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

czarcrab
  • 111
  • 1
  • 2
  • 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 – Olivier Refalo Oct 11 '12 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

Archie
  • 4,959
  • 1
  • 30
  • 36
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

Mike Mitterer
  • 6,810
  • 4
  • 41
  • 62
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

Mike Kormendy
  • 3,389
  • 2
  • 24
  • 21
Robin
  • 111
  • 2
  • 8
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

Hugh Brien
  • 91
  • 1
  • 7
  • 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](https://forums.oracle.com/thread/2596908) to get it to work. – chris Nov 25 '13 at 18:05
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"

mmell
  • 2,448
  • 1
  • 24
  • 16
2

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

/usr/libexec/java_home -v 11
Pavel
  • 4,912
  • 7
  • 49
  • 69
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/

user1270392
  • 2,981
  • 4
  • 21
  • 25
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.

azec-pdx
  • 4,790
  • 6
  • 56
  • 87
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
dev
  • 11,071
  • 22
  • 74
  • 122
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
Shirish Singh
  • 797
  • 7
  • 17
  • I recently figured this one out myself and came looking here to share to find you already discovered; had posted on [Ask Ubuntu](https://askubuntu.com/a/1479533/1715621) This is a good solution if `java` is on the `PATH` but when some tool/application actually needs `JAVA_HOME` set. – Darren Bishop Jul 25 '23 at 08: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.

Anjan
  • 1,613
  • 1
  • 19
  • 25
  • 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 – Olivier Refalo Oct 29 '12 at 02: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

Kewin
  • 248
  • 1
  • 4
  • 8
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
Mika
  • 5,807
  • 6
  • 38
  • 83
Mikey
  • 380
  • 4
  • 15
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!

kate
  • 141
  • 1
  • 6
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)
chrismacp
  • 3,834
  • 1
  • 30
  • 37
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

Andrey Moiseev
  • 3,914
  • 7
  • 48
  • 64