What should I set JAVA_HOME environment variable on macOS X 10.6?
Asked Answered
D

15

463

Many Java applications that use shell scripts to configure their environment use the JAVA_HOME environment variable to start the correct version of Java, locate JRE JARs, and so on.

In macOS X 10.6, the following paths seem to be valid for this variable

/Library/Java/Home
/System/Library/Frameworks/JavaVM.framework/Home
/System/Library/Frameworks/JavaVM.framework/Versions/Current

Some of these are symlinks to the actual current VM (as defined in the Java Preference pane).

But which one should be used—or is it okay to use any of them?

Distiller answered 28/8, 2009 at 19:17 Comment(1)
possible duplicate of Where is JAVA_HOME on OSX Lion (10.7) , Mountain Lion (10.8) or Mavericks (10.9)?Beavers
V
882

I just set JAVA_HOME to the output of that command, which should give you the Java path specified in your Java preferences. Here's a snippet from my .bashrc file, which sets this variable:

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

I haven't experienced any problems with that technique.

Occasionally I do have to change the value of JAVA_HOME to an earlier version of Java. For example, one program I'm maintaining requires 32-bit Java 5 on OS X, so when using that program, I set JAVA_HOME by running:

export JAVA_HOME=$(/usr/libexec/java_home -v 1.5)

For those of you who don't have java_home in your path add it like this.

sudo ln -s /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java_home /usr/libexec/java_home

References:

Vaporish answered 28/8, 2009 at 19:36 Comment(23)
Snow Leopard does still have /usr/libexec/java_home. ThanksDistiller
what is this "/usr/libexec/java_home" ?Haggar
@drlobo: It's a tool for figuring out where JAVA_HOME is that ships with Macs.Vaporish
Still valid for MavericksCanned
I like to conditionalize it (in zsh) with if /usr/libexec/java_home &> /dev/null; then export JAVA_HOME=$(/usr/libexec/java_home); fi, so I can use the same .rc file on multiple machines whether they have the JDK installed or not. Unconditionalized; you'll get a warning on each shell startup.Volitive
In Mavericks I had to put it in ~/.bash_profile instead of ~/.bashrc.Gape
thanks especially for the part with the -v extension ;)Abreaction
If you want to share this environment variable with all users, you must update /etc/profileBurhans
Still valid for Yosemite. ;)Cardiogram
I get an error when setting the path: ln: /usr/libexec/java_home: Operation not permitted. Anybody knows what could be the problem?Refinery
I have multiple versions installed. I did what you wrote but it still shows the newest version. Running on El Capitan. Like someone else posted elsewhere, if you don't want it using the newest version, then you will have to rename the directory where it is installed, move it elsewhere or delete it. Seriously, this is 2016 and nobody has bothered to post a comment here on this???Dumah
For fish shell users, use something like the following: alias java7 "set -gx JAVA_HOME (/usr/libexec/java_home -v1.7)" (Works on El Capitan)Limbate
mkyong.com/java/…Kilian
@JakeStoeffler I think that's true for all versions of mac. I'm on Sierra right nowAlonzoaloof
I can not FIND Java path anywhere for Java 8 Update 121. (macOS 10.12.3) Any ideas?Democritus
Works for High Sierra 10.13.1 with modification of version to 9.0 (possibly due to Homebrew)Downwash
High Sierra 10.13.1 check!!Ternary
Works on High Sierra 10.13.4Decimeter
Still valid for MojaveCapps
Still valid for MojaveAngadreme
As mentioned by Kristopher Johnson below, this is officially documented at Technical Q&A QA1170 - Important Java Directories on Mac OS X.Blairblaire
Remember to log in and log out, otherwise you will not see the changePrussiate
Big Sur 11.2.1 check!Fighter
S
43

Also, it`s interesting to set your PATH to reflect the JDK. After adding JAVA_HOME (which can be done with the example cited by 'mipadi'):

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

Add also in ~/.profile:

export PATH=${JAVA_HOME}/bin:$PATH

P.S.: For OSX, I generally use .profile in the HOME dir instead of .bashrc

Standridge answered 8/3, 2010 at 18:55 Comment(5)
For OSX running default bash, I generally use the bash .bash_profile — why do you use than the generic .profile?Kornher
Since .profile is generic, it will work with sh or bash, while .bash_profile is exclusive to bash. Also this isolates settings from apps that use .bash_profile, like MacPorts. But you can safely use any of the two. See more about it hereStandridge
— FYI, the link you provided confirms my suspicion that under bash (ie by default), use of .bash_profile overrides .profile.Kornher
@LeeGee yes, and it does that because .bash_profile is specific, whereas .profile is generic, for all sh variations.Standridge
Since OS/X puts a link to java in your /usr/bin there shouldn't be a need to explicitly add $JAVA_HOME to your path.Yearround
S
23

I am having MAC OS X(Sierra) 10.12.2.

I set JAVA_HOME to work on React Native(for Android apps) by following the following steps.

  • Open Terminal (Command+R, type Terminal, Hit ENTER).

  • Add the following lines to ~/.bash_profile. export JAVA_HOME=$(/usr/libexec/java_home)

  • Now run the following command. source ~/.bash_profile

  • You can check the exact value of JAVA_HOME by typing the following command. echo $JAVA_HOME

The value(output) returned will be something like below. /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home

That's it.

Sharynshashlik answered 21/6, 2017 at 6:2 Comment(3)
This is helpful, thanks. But I believe you missed including the last step, which is then setting JAVA_HOME to the returned value in .bashrc / .bash_profile / .zshrc / etc. Please correct me if I'm mistaken.Protasis
It is not required. In my case, $JAVA_HOME refers $(/usr/libexec/java_home) and $(/usr/libexec/java_home) is internally referred as /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home by the System.Sharynshashlik
Got it. It was required in my case. Thanks.Protasis
M
15

I'm on Mac OS 10.6.8

The easiest solution works for me is simply put in

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

To test whether it works, put in

$ echo $JAVA_HOME

it shows

/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

you can also test

$ which java
Metagalaxy answered 12/2, 2015 at 20:40 Comment(0)
E
14

Nowadays Java seems to be installed in /Library/Java/JavaVirtualMachines

Enfleurage answered 7/5, 2013 at 21:13 Comment(1)
Related: Where is JAVA_HOME on OSX Lion (10.7) or Mountain Lion (10.8)?Mailable
S
9

I tend to use /Library/Java/Home. The way the preferences pane works this should be up to date with your preferred version.

Silverside answered 28/8, 2009 at 19:23 Comment(2)
/bin . and check that it points to the /Library/java and not /System/Library/.. as that one may be outdated.Arnold
This is no longer accurate. On OS X 10.5 and later, use /usr/libexec/java_home to find the path.Crompton
G
8

That above works not any more in YOSEMITE for GRAPHICAL APPLICATIONS! Like eclipse, or anything started with Spotlight. (.bash_profile, launchd.conf works for terminal sessions only.) Before starting eclipse, just open a terminal window, and give out the following command:

launchctl setenv JAVA_HOME /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home

(With your installation path! Perhaps works with $(/usr/libexec/java_home) instead of the full path too.)

View the whole excellent article about the permanent solution here: Setting environment variables via launchd.conf no longer works in OS X Yosemite/El Capitan/macOS Sierra?

Graham answered 11/5, 2015 at 13:52 Comment(0)
E
4

Create file ~/.mavenrc

then paste this into the file

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

test

mvn -v

Ecker answered 28/12, 2016 at 18:7 Comment(1)
I like this approach, because it allows me to change the Java version very easy within the .mavenrc file.Claxton
R
3

It is recommended to check default terminal shell before set JAVA_HOME environment variable, via following commands:

$ echo $SHELL
/bin/bash

If your default terminal is /bin/bash (Bash), then you should use @hygull method

If your default terminal is /bin/zsh (Z Shell), then you should set these environment variable in ~/.zshenv file with following contents:

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

Similarly, any other terminal type not mentioned above, you should set environment variable in its respective terminal env file.

This method tested working in macOS Mojave Version 10.14.6.

Ryals answered 14/4, 2020 at 9:35 Comment(0)
B
2

I've found this stack to help, i was having the same issue and i could fix:

My java path was here:

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

and was needed to put into my .bash_profile:

export JAVA_HOME=\"/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home\"

Hope help

Bland answered 13/10, 2015 at 19:42 Comment(0)
C
2

As other answers note, the correct way to find the Java home directory is to use /usr/libexec/java_home.

The official documentation for this is in Apple's Technical Q&A QA1170: Important Java Directories on OS X: https://developer.apple.com/library/mac/qa/qa1170/_index.html

Crompton answered 27/4, 2016 at 15:1 Comment(3)
This is a duplicate!Blairblaire
I think the inclusion of the link to official Apple documentation makes it not redundant. But if it is, the correct action is to flag it as a Duplicate, not make a comment.Crompton
I made a comment because I cannot mark an answer as a duplicate. Stack Overflow only allows user to mark a question as a duplicate (unfortunately). The link is helpful, but I think a comment to the current answer (or just edit it with the link since you already have the capability) would be better since it concentrates all useful information in one single answer. I believe there was guideline somewhere that Stack Overflow prefers fixing current answers over writing a new one.Blairblaire
P
2

For me maven seems to work off the .mavenrc file:

echo "export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)" > ~/.mavenrc

I'm sure I picked it up on SO too, just can't remember where.

Propagation answered 8/11, 2016 at 19:22 Comment(0)
M
2

Skipping Terminal setup since you mentioned applications, permanent system environment variable set up (works for macOS Sierra; should work for El Capitan too):

launchctl setenv JAVA_HOME $(/usr/libexec/java_home -v 1.8)

(this will set JAVA_HOME to the latest 1.8 JDK, chances are you have gone through serveral updates e.g. javac 1.8.0_101, javac 1.8.0_131)
Of course, change 1.8 to 1.7 or 1.6 (really?) to suit your need and your system

Mani answered 11/6, 2017 at 10:4 Comment(0)
T
0

MacBook Pro machine - MAC OS Ventura 13

I set JAVA_HOME to work with the following the following steps:

  1. Open Command prompt/Terminal (Command+R, type Terminal in search and hit Enter)

  2. Open ~/.bash_profile file (vi ~/.bash_profile)

  3. Add/Update this lines to ~/.bash_profile (export JAVA_HOME=$(/usr/libexec/java_home))

  4. Run the following command (source ~/.bash_profile)

  5. Verify the java home path (echo $JAVA_HOME)

Output: It will be similar to your java path ( /Library/Java/JavaVirtualMachines/applejdk-17.0.5.8.2.jdk/Contents/Home)

Terrilyn answered 27/2, 2023 at 12:3 Comment(0)
L
-1

For Fish Shell users, use something like the following: alias java7 "set -gx JAVA_HOME (/usr/libexec/java_home -v1.7)"

Limbate answered 28/4, 2016 at 1:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.