367

I just purchased a brand new MacBook Pro.

This is my first MAC ever and I'm still trying to get the hang of navigating my way around.

Anyway, I'm also new to Java and I've been practicing on my Windows PC before it permanently died.

Now that I'm on this MAC, I installed my JDK and now I need to set the JAVA_HOME environment variable.

I have no idea what to do.

I tried following some of these guides and didn't get very far.

  1. Mkyong.com : How to set JAVA_HOME variable in Mac OSX

  2. YouTube : How to set environment variables on mac, linux, solaris, rhel

  3. YouTube : How to Set Environment Variables in Mac

I was able to locate the terminal and I think I created some multiple files. I'm getting messages like this:


(1) Another program may be editing the same file. If this is the case, be careful not to end up with two different instances of the same file when making changes. Quit, or continue with caution.


(2) An edit session for this file crashed. If this is the case, use ":recover" or "vim -r /Users/Erwin/.bash_profile" to recover the changes (see ":help recovery"). If you did this already, delete the swap file "/Users/Erwin/.bash_profile.sw p" to avoid this message.

Can somebody tell how to set Java in Mac OSX environment step by step?

5
  • 2
    Duplicate of: superuser.com/questions/240100/…
    – Frank V
    Commented Apr 3, 2014 at 15:52
  • Possible duplicate of What should I set JAVA_HOME to on OSX
    – Pascal
    Commented Jan 27, 2016 at 21:55
  • briefly explain: stackoverflow.com/a/59151321/5788247
    – Samiran
    Commented Dec 3, 2019 at 6:46
  • 2
    After installing via brew on an M1 Mac, mine was located /opt/homebrew/Cellar/openjdk/17.0.1_1/libexec/openjdk.jdk/Contents/Home. Obviously change for your version but the typical methods of finding the path proved difficult so I hope this helps others. Commented Jan 21, 2022 at 18:40
  • Don't forget to restart the terminal in mac after setting any environment variable, else you keep seeing the unset values Commented Sep 10, 2022 at 9:09

20 Answers 20

632

If you're using bash, all you have to do is:

echo export "JAVA_HOME=\$(/usr/libexec/java_home)" >> ~/.bash_profile

If you're using zsh (which probably means you're running macOS Catalina or newer), then it should instead be:

echo export "JAVA_HOME=\$(/usr/libexec/java_home)" >> ~/.zshrc

In either case, restart your shell.

If you have multiple JDK versions installed and you want it to be a specific one, you can use the -v flag to java_home like so:

echo export "JAVA_HOME=\$(/usr/libexec/java_home -v 1.7)" >> ~/.bash_profile
19
  • 4
    When you say "shell," do you mean I close the terminal window and reopen it? Also, does this permanently add the environment variable for my jdk? Commented Apr 3, 2014 at 15:57
  • 35
    I think instead of restarting the terminal if you do source ~/.bash_profile it would work, right? Commented Sep 11, 2014 at 18:27
  • 5
    @ShobhitPuri Yup, it would, I just wanted to keep it simple for the asker. Commented Sep 11, 2014 at 18:36
  • 6
    echo export "JAVA_HOME=\$(/usr/libexec/java_home -v 1.7)" >> ~/.bash_profile If you need a specific version. 1.7 in this case.
    – sebastian
    Commented Jun 15, 2015 at 22:14
  • 5
    on OSX 11. Instead of bash profile, we're now using .zshrc. So the command will be echo export "JAVA_HOME=\$(/usr/libexec/java_home)" >> ~/.zshrc
    – GusDeCooL
    Commented Sep 24, 2021 at 0:47
151

I just spent 2 hours setting this variable. The other answers did not work properly for me. I'm using macOS Catalina 10.15.4.

First, find your actual Java SDK Home directory:

/usr/libexec/java_home

Manually navigate there to make sure you don't have any mistakes due to incorrect versions, etc. For me, this was:

/Library/Java/JavaVirtualMachines/jdk-13.0.2.jdk/Contents/Home

Next, edit your terminal's profile. If you're using zsh, this will be:

vim ~/.zshrc

If you're not using zsh, this will be:

vim ~/.bash_profile

Inside, add the following new line anywhere in the file:

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-13.0.2.jdk/Contents/Home

Restart your terminal app (or source ~/.bash_profile), and it should work properly.

7
  • 4
    Thank you. This worked for me on MacOS BigSur Commented May 17, 2021 at 19:11
  • 1
    I had the same experience, and this got it solved for me. Thanks!
    – SeanOlson
    Commented Aug 30, 2021 at 14:48
  • 7
    This worked even on macOS Monterey. Thanks for this one! Appreciate it!
    – pragapraga
    Commented Jan 17, 2022 at 9:15
  • To find your JAVA SDK Home directory, you can run these commands too: For MAC OS: echo $JAVA_HOME For Windows: echo %JAVA_HOME% Commented Sep 30, 2022 at 11:53
  • 1
    I ran brew update and brew upgrade which updated my JDK minor version. This caused my JAVA_HOME value to be wrong. Correcting the path to match the new version caused it to work fine now. Commented Dec 5, 2022 at 20:22
59

I did it by putting

export JAVA_HOME=`/usr/libexec/java_home`

(backtics) in my .bashrc. See my comment on Adrian's answer.

Additionally update PATH variable as well.

PATH=$PATH:$JAVA_HOME/bin
2
  • 1
    Slightly more modern way: export JAVA_HOME="$(/usr/libexec/java_home)"
    – MarkHu
    Commented Nov 29, 2022 at 22:15
  • I have added path for jdk 17 but when i check it shows 22.0.2 java version, can anyone help me to set up java version to 17 and remove latest version.
    – user26611416
    Commented Aug 9 at 10:24
46

Set $JAVA_HOME environment variable on latest or older Mac OSX.

Download & Install install JDK

  1. First, install JDK
  2. Open terminal check java version

$ java -version

Set JAVA_HOME environment variable

  1. Open .zprofile file

$ open -t .zprofile

Or create . zprofile file

$ open -t .zprofile

  1. write in .zprofile

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

Save .zprofile and close the bash file & then write in the terminal for work perfectly.

$ source .zprofile

Setup test in terminal

$ echo $JAVA_HOME  
/Library/Java/JavaVirtualMachines/jdk-13.0.1.jdk/Contents/Home
6
  • 7
    Also I had a question what "/usr/libexec/java_home" actually is. Answer: medium.com/zendesk-engineering/…
    – ekar
    Commented Feb 7, 2020 at 18:28
  • 4
    /usr/libexec/java_home is the simplest maintainable way of setting JAVA_HOME on macOS. You can use java_home to: Find all installed JDKs.
    – Samiran
    Commented Feb 8, 2020 at 11:17
  • this works for me on a 2019 Mac running Catalina in 2020. Accepted and most upvoted answer didn't. First thing is that the bash profile is now .zprofile, also the export command content in accepted answer didn't work for me but this did.
    – cryanbhu
    Commented Sep 23, 2020 at 8:03
  • Just a note about where to get the JDK from–alternatively, you can install JDK from adoptopenjdk.net. The difference is that Oracle's licence does not allow commercial builds using its JDK unless you pay for the licence. Whereas adoptopenjdk is open source.
    – emil.c
    Commented Jan 12, 2021 at 16:56
  • export PATH="${JAVA_HOME}/bin:$PATH"
    – Konchog
    Commented Feb 22 at 13:25
41

In Mac OSX 10.5 or later, Apple recommends to set the $JAVA_HOME variable to /usr/libexec/java_home, just export $JAVA_HOME in file ~/.bash_profile or ~/.profile.

Open the terminal and run the below command.

$ vim .bash_profile

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

Save and exit from Vim editor, then run the source command on .bash_profile

$ source .bash_profile

$ echo $JAVA_HOME

/Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home
2
  • I have added path for jdk 17 but when i check it shows 22.0.2 java version, can anyone help me to set up java version to 17 and remove latest version.
    – user26611416
    Commented Aug 9 at 10:24
  • 1
    @MoonPrincess You can specify this way to specific java version export JAVA_HOME=$(/usr/libexec/java_home -v 17.x.x)
    – Rajesh
    Commented Aug 21 at 17:24
28

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 @Adrian Petrescu 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.

1
  • export JAVA_HOME=$(/usr/libexec/java_home) export PATH="${JAVA_HOME}/bin:$PATH"
    – Konchog
    Commented Feb 22 at 13:24
17

Quick Guide for M1

  1. Add java sdk into your m1 check version

    java --version

Get all java versions installed in ur mac

/usr/libexec/java_home -V
  1. Execute for Java path from library

    /usr/libexec/java_home

(specify java version if you have multiple version, In my case -v17.0.5

/usr/libexec/java_home -v17.0.5
  1. Mac>User>'YourUserName/Home'>.zshrc

    export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-17.0.5.jdk/Contents/Home

2
  • 2
    this works best for M1!
    – manhnt
    Commented Aug 10, 2023 at 18:28
  • there is no libexec on my mac
    – Waldmann
    Commented Aug 23 at 3:38
7

If you are using Zsh, then try to add this line in ~/.zshrc file & restart terminal.

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

JAVA 11 via Homebrew - tested on macos Ventura 2022

.zshrc

export JAVA_HOME=/opt/homebrew/opt/openjdk@11/libexec/openjdk.jdk/Contents/Home
4

I got it working by adding to ~/.profile. Somehow after updating to El Capitan beta, it didnt work even though JAVA_HOME was defined in .bash_profile.

If there are any El Capitan beta users, try adding to .profile

3
  • 2
    What's the difference between .profile and .bash_profile? Commented Nov 29, 2015 at 17:17
  • 4
    The idea is that .profile runs for all shells (sh, zsh, whatever); .bash_profile, only for bash shells.
    – JohnL4
    Commented Jul 30, 2016 at 15:46
  • .profile is your personal profile file regardless of which shell you use. Commented Jun 16 at 5:19
4

More simply on a mac terminal with a modern OSX

$ vim ~/.zshrc

Type "a" to being editing, and then paste (ctrl + v):

$ JAVA_HOME=/usr/libexec/java_home

then hit "escape" and type exactly ":wq" in order to write to the file and quit vim mode.

Finally, when out of vim mode and back in your terminal, type

$ source ~/.zshrc

This will refresh so that your terminal is aware of the changes.

  • IMPORTANT * If you don't "source" the file, you won't see the changes in this terminal session.

Check the changes by typing

$ echo $JAVA_HOME

and you should see /usr/libexec/java_home

3
  • 3
    Please correct: JAVA_HOME=/usr/libexec/java_home should be export JAVA_HOME=$(/usr/libexec/java_home)
    – MarkHu
    Commented Dec 1, 2022 at 2:16
  • There was a typo @MarkHu. I fixed it. Apologies, and thanks for pointing that out.
    – Forrest
    Commented Mar 2, 2023 at 15:08
  • there is no .zshrc on my mac
    – Waldmann
    Commented Aug 23 at 3:42
3

For Mac M1

Download & Install install JDK

Open terminal check java version

java -version

Now create a file

touch .zprofile

Open the file

open -t .zprofile

Add the below line

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

1

Since I'm using openjdk managed with sdkman, I added

sudo ln -sfn /path/to/my/installed/jdk/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk

Adding this to your system lets java_home recognize your installed version of Java even when its not installed via standard packages

2
  • What do you set JAVA_HOME to in this case, please? export JAVA_HOME="/usr/libexec/java_home" ?
    – Houman
    Commented Dec 12, 2020 at 10:02
  • With that line java will be discovered similarly to if it were a standard installation, we're linking the standard install location to point to the actual install location, so its treated like an officially installed package and you don't need to set JAVA_HOME Commented Dec 13, 2020 at 19:10
1

In the latest Mac, you have to add the Set $JAVA_HOME environment variable in .zprofile. Here, we simple way to open it. Press ⌘ + Shift + . from keyboard. Just open it and add the $JAVA_HOME environment variable as explained here:

1

I'm able to solve this issue by setting JAVA_HOME in .bash_profile file

export JAVA_HOME=/usr/local/opt/openjdk@17

Note: I installed openjdk version 17 using 'brew'. I got this location from brew console. I'm using 'bash' instead of 'zsh' in my mac.

1
  1. i want to update JAVA_HOME variable ,so it is picked by maven in mac
  2. i have used brew to install openjdk11 on my mac
  3. i have a /.zshrc file in my mac

so i used below to set the JAVA_HOME to my openjdk11 -

  1. vim ~/.zshrc
  2. add below lines export=/opt/homebrew/opt/openjdk@11
0

I resolved it on macOS Monterey by using the option provided by Google

Under Gradle JDK, choose the Embedded JDK option.

https://developer.android.com/studio/intro/studio-config#:~:text=A%20copy%20of%20the%20latest,use%20for%20your%20Android%20projects.

0
  • Open Terminal.
  • Confirm you have JDK by typing “which java”. ...
  • Check you have the needed version of Java, by typing “java -version”.
  • Set JAVA_HOME using this command in Terminal: export JAVA_HOME=/Library/Java/Home.
  • echo $JAVA_HOME on Terminal to confirm the path.
0

I had multiple versions of JDK installed, so what worked for me is that I manually configured my SpringToolSuite4 to use Java 11

-1

For mac users https://stackoverflow.com/a/77953587/4792285

Install using homebrew https://formulae.brew.sh/formula/openjdk

brew install openjdk@21 

OR

brew install openjdk@11

Then set up the .profile on your mac (/Users/your_username/.profile). If this file is hidden you may need to press Command + Shift + . (the period key) to make it visible.

add JAVA_HOME and update the PATH variable

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

open a new terminal and type

echo $JAVA_HOME
echo $PATH

to verify your change.

https://stackoverflow.com/a/77953587/4792285

1
  • homebrew can create very messy collection of lost and obsolete JDKs. if you're a developer, it's best to avoid brew for JDKs. set it up yourself to be sure you know which JDK will be used.
    – devdanke
    Commented Aug 11 at 11:45

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