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

27

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.

Torch answered 5/7, 2011 at 20:19 Comment(3)
Possible duplicate of What should I set JAVA_HOME to on OSXBrietta
On Mac Sierra I've /Library/Java/JavaVirtualMachines which has jdk and System Preferences > Java what's different between these 2 ?Berman
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!Vantage
T
1097

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.

Torch answered 5/7, 2011 at 20:21 Comment(16)
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.Economizer
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...Economizer
@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.Economizer
this didn't work for me. /System/Library/Frameworks/JavaVM.framework/Home worked.Samoyedic
The backticks were my undoing. This worked after copying and pasting properly :)Vadavaden
Works on Maverick, too!Multiflorous
For me @chaostheory's solution worked. On OSX Mavericks.Stutter
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/binCopywriter
@Samoyedic I tried this but it says "... is a directory"Rhys
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).Elementary
Works on Yosemite. Be careful with the ticking as what @Vadavaden mentioned, copy & paste does the trick.Saprophagous
Worked for me. I had to restart Terminal before the difference took effect.Poultice
also found this link which may help as well: gist.github.com/johan/10590467Pradeep
Works on Mac OS X High Sierra 10.13.6Genovevagenre
For Java 11 and High Sierra, export JAVA_HOME="$(/usr/libexec/java_home -v 11)"Ouabain
The double quotes are not requiredApplewhite
C
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.

Clustered answered 15/8, 2012 at 10:28 Comment(2)
blog.hgomez.net/2012/07 is broken but it's fixed in the answer which now links to web.archive.org/web/20140813164713/http://blog.hgomez.net/blog/…Musculature
export JAVA_HOME="/usr/libexec/java_home -v 1.8"Cowboy
C
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.

Cindacindee answered 29/12, 2012 at 23:0 Comment(4)
It would have worked if you had installed java 6 from Apple. the point here is that you are using oracle java 7.Torch
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)"Streetman
I tried to set up maven on mac Yosemite, this way to set JAVA_HOME works for me.Mccully
Works for me on Sierra 10.12 and Java 8Monk
U
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.

Uproar answered 15/6, 2015 at 1:30 Comment(6)
I think I got your pb - you use quotes instead of backquotes ' vs `Torch
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.Uproar
@OlivierRefalo I can see you've updated your answer with double quotes("). But even without quotes, it works.Uproar
Thanks a lot . It worked for me to in Yosemite 10.10Train
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.Moureaux
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 installedUnderstate
V
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'
Venola answered 22/9, 2017 at 6:54 Comment(0)
T
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.

Trahurn answered 29/11, 2011 at 14:7 Comment(4)
Was it lion ? did you get the official Java package from Apple, or Java6/7 from Oracle ?Torch
I believe it was Apple's Java package.Trahurn
@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.jarDysgenics
Thanks for the info. Maybe I'm too simple, but why does OS X make such a simple thing a complete mess?Hypothesis
O
19

For OS X you can do:

export JAVA_HOME=`/System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java_home`
Of answered 16/9, 2014 at 16:0 Comment(1)
Of note, HFS+ is case insensitive.Of
S
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
Spandex answered 27/1, 2012 at 7:2 Comment(2)
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/HomeTorch
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.Spandex
P
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"
Person answered 28/10, 2014 at 2:39 Comment(4)
How is that different that the proposed solution?Torch
It's not different to the proposed solution. This solution is specific to people using ZSH, Java 8 and OSX Yosemite.Person
Confirmed working like a charm with java 1.8 installed on OSX Yosemite, cheers mate.Africanist
The general solution works just fine for Java 8, zsh, and Yosemite.Hegarty
Q
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)
Quent answered 12/11, 2018 at 6:32 Comment(1)
Works on Catalina. TyRobinetta
B
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

Blandishments answered 11/10, 2012 at 15:4 Comment(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 matchTorch
M
11

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

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

Miltiades answered 3/5, 2013 at 16:43 Comment(3)
Indeed - and the /usr/libexec/java_home tool returns an error for these Java versions.Enrollment
This is THE only answer working for me after downloading v1.7 JREDioecious
This is the path if you install the JRE. Most of the rated answers and comments are about the JDK. Not the same thing.Replication
M
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

Mucin answered 16/10, 2013 at 10:45 Comment(0)
E
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

Enrika answered 29/12, 2018 at 17:42 Comment(0)
R
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

Revolution answered 25/10, 2013 at 23:36 Comment(1)
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.Map
S
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"

Scantling answered 19/11, 2014 at 19:26 Comment(0)
T
2

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

/usr/libexec/java_home -v 11
Tiphanie answered 9/1, 2020 at 9:15 Comment(0)
A
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/

Apia answered 23/4, 2013 at 17:33 Comment(0)
B
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.

Bastardy answered 10/11, 2014 at 7:50 Comment(0)
G
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
Gravelblind answered 15/8, 2019 at 17:5 Comment(0)
S
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
Sortition answered 5/9, 2022 at 8:49 Comment(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.Intent
S
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.

Sinistral answered 28/10, 2012 at 19:53 Comment(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 thoseTorch
W
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

Wordbook answered 13/5, 2015 at 14:53 Comment(0)
O
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
Ogle answered 31/5, 2015 at 5:22 Comment(1)
Can you please add more information to make this answer useful.Runagate
N
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!

Nanceenancey answered 4/10, 2015 at 10:14 Comment(0)
A
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)
Alvarado answered 6/6, 2018 at 13:39 Comment(0)
C
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

Chloro answered 7/10, 2018 at 12:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.