399

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?

6
  • 2
    Duplicate of: superuser.com/questions/240100/… Commented Apr 3, 2014 at 15:52
  • Possible duplicate of What should I set JAVA_HOME to on OSX Commented Jan 27, 2016 at 21:55
  • briefly explain: stackoverflow.com/a/59151321/5788247 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
  • 1
    See stackoverflow.com/questions/22842743/…. The idea is to approach this step by step so that minor revisions to OSs dont matter & the OP is able to troubleshoot seamlessly and fix if the core concepts are understood. Commented Sep 26 at 13:52

25 Answers 25

670

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
Sign up to request clarification or add additional context in comments.

19 Comments

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?
I think instead of restarting the terminal if you do source ~/.bash_profile it would work, right?
@ShobhitPuri Yup, it would, I just wanted to keep it simple for the asker.
echo export "JAVA_HOME=\$(/usr/libexec/java_home -v 1.7)" >> ~/.bash_profile If you need a specific version. 1.7 in this case.
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
|
161

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.

6 Comments

I had the same experience, and this got it solved for me. Thanks!
This worked even on macOS Monterey. Thanks for this one! Appreciate it!
To find your JAVA SDK Home directory, you can run these commands too: For MAC OS: echo $JAVA_HOME For Windows: echo %JAVA_HOME%
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.
this works for me on i use macOS Monterey version 12.66.8
|
61

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 Comments

Slightly more modern way: export JAVA_HOME="$(/usr/libexec/java_home)"
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.
49

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 Comments

Also I had a question what "/usr/libexec/java_home" actually is. Answer: medium.com/zendesk-engineering/…
/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.
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.
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.
export PATH="${JAVA_HOME}/bin:$PATH"
|
43

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 Comments

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.
@MoonPrincess You can specify this way to specific java version export JAVA_HOME=$(/usr/libexec/java_home -v 17.x.x)
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 Comment

export JAVA_HOME=$(/usr/libexec/java_home) export PATH="${JAVA_HOME}/bin:$PATH"
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 Comments

this works best for M1!
there is no libexec on my mac
7

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

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

Comments

6

JAVA 11 via Homebrew - tested on macos Ventura 2022

.zshrc

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

Comments

5

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

4 Comments

Please correct: JAVA_HOME=/usr/libexec/java_home should be export JAVA_HOME=$(/usr/libexec/java_home)
There was a typo @MarkHu. I fixed it. Apologies, and thanks for pointing that out.
there is no .zshrc on my mac
@Waldmann you can just create one.
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 Comments

What's the difference between .profile and .bash_profile?
The idea is that .profile runs for all shells (sh, zsh, whatever); .bash_profile, only for bash shells.
.profile is your personal profile file regardless of which shell you use.
4

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)

Comments

3

First for MacOS let's check the java path go to finder:

command + space 

type

finder

on the finder press the following key to open goto:

command+shift+g

and you can go to this path below, depending upon the java version, you can go to here;

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

In case you didn't find java on this path: /Library/Java/JavaVirtualMachines/jdk-23.jdk/Contents/Home/

then you need to install java sdk for macOS, You can find it in java official page, and install ARM64 DMG Installer, based on your macOS.

After this check again this path as with above steps:

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

then add this to your .zshrc file,

in case you don't have .zshrc file, go to terminal type

cd 

then type

ls -al

still you didn't see the .zshrc file then make one using

nano ~/.zshrc

and press i then insert the path

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk23.jdk/Contents/Home

press escape :x and hit enter to save

now include this in source by typing following from the terminal:

source ~/.zshrc

let's check if it's saved or not, type

vim ~/.zshrc

again press i and press :x to exit

This should work!!!

if this doesn't work then make

nano ~/.bash_profile 

and do the same process as we did.

my ~/.zshrc file looks like this:

export PATH="/opt/homebrew/bin:$PATH"
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-23.jdk/Contents/Home
export ANDROID_HOME=$HOME/Library/Android/sdk
export PATH=$PATH:$ANDROID_HOME/platform-tools
export PATH=$PATH:$ANDROID_HOME/tools
export PATH=$PATH:$ANDROID_HOME/tools/bin
export PATH=$PATH:$ANDROID_HOME/emulator

Comments

2

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 Comments

What do you set JAVA_HOME to in this case, please? export JAVA_HOME="/usr/libexec/java_home" ?
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
2

It has been 25 years of Java & Mac OS, both very popular systems but a basic issue like JAVA_HOME path setting still throws occassional errors. I give a step by step understanding here in addition to solution(s) so that users can troubleshoot themselves. Note that no Admin access or SUDO access is needed for this.

Let us understand the PATH variable first. Open your Terminal and type: echo $PATH
The terminal echos the value of the variable named PATH. In unix "$" is used to precede variable names (so echo PATH will just echo the letters PATH). Anyway, what is this $PATH? It is an environment variable that contains a colon-separated list of directories where your shell will look for executables that execute certain terminal commands. This helps because then you dont have to specify the full path to the runtime libraries each time you want to execute something. It makes sense to point out this path only once. This is where PATH entries help. So our goal is to set up a similiar entry for the Java run time so that the terminal shell knows where to look for Java runtime libraries to execute the Java file you want. How do we do that and where is the Java installation on the Mac?

Usually, there is lot of confusion about where the right Java installation is. MacOS thankfully can show us the installation path with a simple command. Just type /usr/libexec/java_home in the Terminal. You will get the installation path. It will be something like "/Library/Java/JavaVirtualMachines/SOME_JDK_VERSION/ Contents/Home". Just copy that path value & do some hardcoding in terminal prompt as follows: export JAVA_HOME=the path you got above

Now you are all set. Actually half-set because the moment you close this terminal session, this value will be reset. The value is not persistent. Also secondly people often frown upon harcodings. Let us fix the second issue first.

Some solutions above suggest to do export JAVA_HOME="/usr/libexec/java_home" or export JAVA_HOME=/usr/libexec/java_home instead of hardcoding the path. The intention is good but it does not work. Instead do this: export JAVA_HOME=$(/usr/libexec/java_home). The $ tells the shell - The value of this expression is the output from executing the command in brackets (i.e. "/us/libexec/java_home"). This fixes the hardcoding issue. Before we go into the other issue (the one of persistence of the PATH value scross sessions), it is imperative to point out one more source of error. Some solutions above suggest to prepend the $HOME variable to the path: export JAVA_HOME=$H0ME/Library/Java/JavaVirtualMachines/YOUR_JDK_VERSION_COMES_HERE/Contents/Home. $HOME is your home directory. You can just try doing echo $HOME in terminal to see what it is. So when we precede the path by $HOME, it is like giving the entire full path. For some reason, this does not work in the OS version I have. Perhapes the OS by default looks under your Home directory & now this smart addition of the extra $HOME causes it to thrown an error. So please avoid this.

Now let us address the persistence issue. In latest Macs, when you open Terminal, the Z-shell zsh opens up. It is the Unix shell that latest MacOS uses & is the default shell program. .zshrc is the configuration file for zsh whenever you open a new terminal window. We can store some configuration stuff there. In fact that is where we will define this JAVA_HOME guy. (Note that we could also store this in the .zprofile file but we will stick to .zshrc). This file's full path is ~/.zshrc. The symbol ~ means the home directory of the logged in user. So you could open the .zshrc file by searching for it in the hard drive of your Mac in your home directory (make sure to enable hidden files to be displayed). Open and manually edit to add an entry for JAVA_HOME. Alternatively, in terminal, we can just do open ~/.zshrc. This opens it up. Manually add the entry:
export JAVA_HOME=$(/usr/libexec/java_home). Make sure there are no blank spaces before or after the "=" sign & also press carriage return (Enter) after the last line. Save. RESTART Terminal (we know why right?). Once you start Terminal, the configuration file runs and sets up everything automatically. Now your Java progrm runs like a charm.

Alternatively, you can hardcode the path in the file by adding: export JAVA_HOME=/Library/Java/JavaVirtualMachines/YOUR_JDK_VERSION_COMES_HERE/Contents/Home. I just gave an indicative path. You need to replace the entire path with the path you got when you typed /usr/libexec/java_home in the Terminal. Don't try prepending with $HOME or other funny business. It is good that you are trying to help by being thorough and providing the complete path but for some reason the (latest) Mac OS (as of Sep 2025) did not appreciate this good intention.

Other issues:

(1) There is no JDK. well, you got to install that first.

(2) There is no .zshrc file. Well, go ahead & create one & add the above line. use the command touch ~/.zshrc to create the file

(3) Multiple versions of Java exist. Use /usr/libexec/java_home -V and select & harcode the path you want. Don't worry about the frowners. Get your system up & running. You can later re-visit this setting when you are more comfortable.

(4) Not the latest Mac OS? Then maybe zsh is not the default editor? Maybe it is bash? Well, no issue cause that one too has a configuration file. Let us look at an interesting way to update that file directly. echo export "JAVA_HOME=\$(/usr/libexec/java_home)" >> ~/.bash_profile. Hey, what is this cool trick? This hack can apply to zsh also. For e.g.:
echo export "JAVA_HOME=$(/usr/libexec/java_home)" >> ~/.zshrc
The >> redirects output of the command preceeding it to the file .zshrc located at ~. It simply appends that output to the end of the file. So this command when executed in Terminal does what we want i.e. set the value of JAVA_HOME in the .zshrc file so that Terminal is able to automatically find the Java runtime libraries. This too works like a charm except that it hardcodes the path directly into the file (which is ok).

Bonus: Now that you have set up the path to Java runtime libraries, you can execute Java files in Terminal. This setting may or may not be read by your IDE. For e.g., I was trying to set up Java in VSCode. Since VSCode is a general IDE you need an "extension" for Java. Microsoft magnanimously has put Oracle's Java extension at the top of the extension search results. So I installed that one by default. It needed me to make an additional setting for JDK path as part of a 1-time configuration set up. There is one more "extension" for Java by Microsoft itself. This one doesent need any config setup and works right off the shelf. Happy coding

2 Comments

Can you format your code/commands a bit for better readability?
Done. Looks like stackoverflow has changed their editor and I was bit un-used to the new one. Anyway I formatted it a bit and corrected some minor mistakes.
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:

Comments

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.

Comments

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

Comments

1

Adrian Petrescu's answer is correct.
https://stackoverflow.com/a/22842806/10090030
Additionally, you can verify your changes by running the following command in the terminal:

print $JAVA_HOME

If the changes were applied successfully, you'll see:

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

If the configuration is incorrect, nothing will be displayed.

Comments

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.

Comments

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.

Comments

0

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

Comments

0

If you already have JDKs installed, to find those SDK paths, you can use below command -

/usr/libexec/java_home -V

It will give output like,

enter image description here

Just copy the path, and add it in the .zshrc file,

export JAVA_HOME=$HOME/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home

save the file and run,

source ~/.zshrc

JAVA_Home will be set.

If you don't have JDK installed then you can use sdkman for it

# install sdkman
curl -s "https://get.sdkman.io" | bash

# Show the list of jdks available
sdk list java 

# Install the specific JDK
 sdk install java 17.0.14-jbr

Comments

0

SDKMAN!

Install the SDKMAN! utility on your Mac.

Installing Java sets JAVA_HOME

Get a list of available JDKs published by various vendors. (Be sure to research their legal license terms.)

sdk list java
  1. From that list, copy the name of the JDK product of your choice.
  2. Call sdk install java command, passing the name of your JDK product.
sdk install java your-choice-of-jdk-goes-here

The installer asks if you want to make that your default JDK. If you agree, SDKMAN! sets the JAVA_HOME environment variable.

Changing JAVA_HOME

To later specify a particular JDK as the default, run the sdk default java command. SDKMAN! sets the JAVA_HOME environment variable.

sdk default java your-choice-of-jdk-goes-here

Setting JAVA_HOME temporarily

To set the default JDK within the current terminal shell session only, rather than across all future shell sessions, call the sdk use java command.

sdk use java your-choice-of-jdk-goes-here

Comments

-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 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.