227

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?

melwil
  • 2,508
  • 1
  • 18
  • 31
islander_zero
  • 2,544
  • 2
  • 11
  • 17
  • Duplicate of: http://superuser.com/questions/240100/setting-java-home-environment-variable – Frank V Apr 03 '14 at 15:52
  • 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) – Pascal Jan 27 '16 at 21:55
  • briefly explain: https://stackoverflow.com/a/59151321/5788247 – Shomu Dec 03 '19 at 06:46
  • 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. – Joshua Pinter Jan 21 '22 at 18:40

10 Answers10

447

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
Adrian Petrescu
  • 15,183
  • 6
  • 55
  • 79
  • 2
    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? – islander_zero Apr 03 '14 at 15:57
  • 1
    Yes, that's what I mean. And yes, that will add the variable for all login shells for your user (i.e, pretty much all of the circumstances in which you would want it) – Adrian Petrescu Apr 03 '14 at 16:00
  • 24
    I think instead of restarting the terminal if you do `source ~/.bash_profile` it would work, right? – Shobhit Puri Sep 11 '14 at 18:27
  • 3
    @ShobhitPuri Yup, it would, I just wanted to keep it simple for the asker. – Adrian Petrescu Sep 11 '14 at 18:36
  • 1
    Is there some sort of reference document from which this black magic can be obtained? (Answer: yes. 'man java_home'. Apparently part of Xcode. ('xcode-select -p' to determine whether you have it.)) Me: another Mac noob. – JohnL4 Oct 19 '14 at 23:27
  • 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 Jun 15 '15 at 22:14
  • Will this update to the latest version with future java updates? Or do you need to re-run each time? – Rarw Sep 03 '17 at 17:01
  • 1
    @Rarw: If you put this in your `~/.bash_profile`, it'll run every time you log in. So, no need to change anything :) – Adrian Petrescu Sep 04 '17 at 23:36
  • the `export` content didnt work for my Mac. Running Catalina in 2020 another answer worked for me – cryanbhu Sep 23 '20 at 08:01
  • 1
    Before doing this, check the shell that you are using echo $SHELL.... If it is zsh, instead of bash_profile, you would have to use .zshenv or .zshrc – Kushan Aug 12 '21 at 11:22
  • 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 Sep 24 '21 at 00:47
  • `~/.bash_profile` does not work on macOS Monterey version 12.0. `echo export "JAVA_HOME=\$(/usr/libexec/java_home)" >> ~/.zshrc` can work. – Instein Nov 23 '21 at 18:06
  • I received the message after run this command echo export "JAVA_HOME=\$(/usr/libexec/java_home)" >> ~/.zshrc source .zshrc .zshrc:export:118: JAVA_HOME: inconsistent type for assignment – Pavlo Chechehov Dec 23 '21 at 15:55
  • we must source the bash zsh file. – artist.pradeep Mar 18 '22 at 09:31
50

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.

Koray Tugay
  • 21,794
  • 41
  • 171
  • 299
Steve
  • 719
  • 6
  • 7
37

I did it by putting

export JAVA_HOME=`/usr/libexec/java_home`

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

JohnL4
  • 908
  • 8
  • 17
28

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
Levi Arista
  • 295
  • 1
  • 3
  • 13
Rajesh
  • 3,505
  • 1
  • 30
  • 31
23

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
Community
  • 1
  • 1
Shomu
  • 2,229
  • 19
  • 32
  • 3
    Also I had a question what "/usr/libexec/java_home" actually is. Answer: https://medium.com/zendesk-engineering/setting-java-home-on-mac-os-acd44e3a6027 – ekar Feb 07 '20 at 18:28
  • 3
    /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. – Shomu Feb 08 '20 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 Sep 23 '20 at 08:03
  • Just a note about where to get the JDK from–alternatively, you can install JDK from https://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 Jan 12 '21 at 16:56
17

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.

Jerry Chong
  • 5,682
  • 2
  • 36
  • 34
5

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

export JAVA_HOME=$(/usr/libexec/java_home) 
Niket Shah
  • 311
  • 4
  • 4
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

1

I checked my /Library/Java/JavaVirtualMachines/ directory and found the version to be jdk1.8.0_321.jdk/Contents/Home and added this directly to my .bash_profile:

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home

but it's still complaining after sourcing the .bash_profile:The operation couldn’t be completed. Unable to locate a Java Runtime that supports apt.

ptn77
  • 487
  • 2
  • 7
  • 18
  • sorry, I was using the sudo apt install for linux and had to change it to brew. That was my problem – ptn77 Feb 21 '22 at 19:36
  • 2
    Even though a different user upvoted this I do not see how this answers the question at the top of this page, but it should. Please [edit] according to [answer] or delete the answer. Otherwise it risks being flagged as "not an answer" and being deleted. – Yunnosch May 02 '22 at 06:22
0

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

  • What do you set JAVA_HOME to in this case, please? `export JAVA_HOME="/usr/libexec/java_home"` ? – Houman Dec 12 '20 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 – John Marcus Dec 13 '20 at 19:10