How to Set $JAVA_HOME environment variable on macOS

This article shows how to set the $JAVA_HOME environment variable on older Mac OS X and the latest macOS 11.

Topics

  1. macOS release history
  2. What is /usr/libexec/java_home
  3. $JAVA_HOME and macOS 11 Big Sur
  4. $JAVA_HOME and Mac OS X 10.5 Leopard
  5. $JAVA_HOME and older Mac OS X
  6. Switch between different JDK versions

Solution
Steps to set the $JAVA_HOME environment variable on macOS.

  1. Find out your macOS version.
  2. Find out which shell you are using, bash or zsh?
  3. For zsh shell, export $JAVA_HOME at ~/.zshenv or ~/.zshrc.
  4. For bash shell, export $JAVA_HOME at ~/.bash_profile or ~/.bashrc.
  5. Test with echo $JAVA_HOME.
  6. Done.

Related
Read this – How to install Java JDK on macOS

1. macOS release history, bash or zsh?

1.1 Review the macOS release history, source Wikipedia – macOS.

  1. Mac OS X Public Beta
  2. Mac OS X 10.0 (Cheetah)
  3. Mac OS X 10.1 (Puma)
  4. Mac OS X 10.2 Jaguar
  5. Mac OS X 10.3 Panther
  6. Mac OS X 10.4 Tiger
  7. Mac OS X 10.5 Leopard
  8. Mac OS X 10.6 Snow Leopard
  9. Mac OS X 10.7 Lion
  10. OS X 10.8 Mountain Lion
  11. OS X 10.9 Mavericks
  12. OS X 10.10 Yosemite
  13. OS X 10.11 El Capitan
  14. macOS 10.12 Sierra
  15. macOS 10.13 High Sierra
  16. macOS 10.14 Mojave
  17. macOS 10.15 Catalina (zsh)
  18. macOS 11 Big Sur (zsh)

1.2 bash or zsh?
On macOS 10.15 Catalina and later, the default Terminal shell switch from the bash (Bourne-again shell) to zsh (Z shell).

  • For bash shell, we can put the environment variables at ~/.bash_profile or ~/.bashrc.
  • For zsh shell, we can put the environment variables at ~/.zshenv or ~/.zshrc.

We can print the $SHELL environment variable to determine the current shell you are using.

Terminal

% echo $SHELL

/bin/zsh

Further Reading

2. What is /usr/libexec/java_home

2.1 On Mac OS X 10.5 or later, we can use /usr/libexec/java_home to return the location of the default JDK.

Terminal

% /usr/libexec/java_home
/Library/Java/JavaVirtualMachines/jdk-16.jdk/Contents/Home  

2.2 Also, find all installed JDKs.

Terminal

% /usr/libexec/java_home -V
Matching Java Virtual Machines (4):
    16 (x86_64) "Oracle Corporation" - "OpenJDK 16-ea" /Library/Java/JavaVirtualMachines/jdk-16.jdk/Contents/Home
    15.0.1 (x86_64) "UNDEFINED" - "OpenJDK 15.0.1" /usr/local/Cellar/openjdk/15.0.1/libexec/openjdk.jdk/Contents/Home
    14.0.2 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 14" /Library/Java/JavaVirtualMachines/adoptopenjdk-14.jdk/Contents/Home
    1.8.0_275 (x86_64) "UNDEFINED" - "OpenJDK 8" /usr/local/Cellar/openjdk@8/1.8.0+275/libexec/openjdk.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/jdk-16.jdk/Contents/Home  

2.3 Also, run a specified JDK command.

Terminal

% /usr/libexec/java_home -v1.8

/usr/local/Cellar/openjdk@8/1.8.0+275/libexec/openjdk.jdk/Contents/Home  

3. $JAVA_HOME and macOS 11 Big Sur

On macOS 10.15 Catalina and later, the zsh is the default Terminal shell, and we can set the $JAVA_HOME environment variable in either ~/.zshenv or ~/.zshrc.

3.1 Open the ~/.zshenv

Terminal

% nano ~/.zshenv

3.2 Add the following content

~/.zshenv

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

3.3 Source the file and print the $JAVA_HOME, done.

Terminal

% source ~/.zshenv

% echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk-16.jdk/Contents/Home  

4. $JAVA_HOME and Mac OS X 10.5 Leopard

For older Mac OS X, the bash is the default Terminal shell, and we can set the $JAVA_HOME environment variable in either ~/.bash_profile or ~/.bashrc.

4.1 Open the ~/.bash_profile

Terminal

% nano ~/.bash_profile

4.2 Add the following content

~/.bash_profile

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

4.3 Source the file and print the $JAVA_HOME

Terminal

% source ~/.bash_profile

% echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk-16.jdk/Contents/Home  

5. $JAVA_HOME and older Mac OS X

On older Mac OS X, the tool /usr/libexec/java_home doesn’t exists, and we need to set the $JAVA_HOME to the real path.

5.1 Open the ~/.bash_profile

Terminal

% nano ~/.bash_profile

5.2 Add the following content

~/.bash_profile

export JAVA_HOME=/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

5.3 Source the file and print the $JAVA_HOME

Terminal

% source ~/.bash_profile

% echo $JAVA_HOME
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

6. Switch between different JDK versions

For example, this macOS contains four JDK: 1.8, 14, 15, and 16, and the default JDK is 16.

Terminal

% /usr/libexec/java_home -V

Matching Java Virtual Machines (4):
  16 (x86_64) "Oracle Corporation" - "OpenJDK 16-ea" /Library/Java/JavaVirtualMachines/jdk-16.jdk/Contents/Home
  15.0.1 (x86_64) "UNDEFINED" - "OpenJDK 15.0.1" /usr/local/Cellar/openjdk/15.0.1/libexec/openjdk.jdk/Contents/Home
  14.0.2 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 14" /Library/Java/JavaVirtualMachines/adoptopenjdk-14.jdk/Contents/Home
  1.8.0_275 (x86_64) "UNDEFINED" - "OpenJDK 8" /usr/local/Cellar/openjdk@8/1.8.0+275/libexec/openjdk.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/jdk-16.jdk/Contents/Home

6.1 For zsh shell, edit the ~/.zshenv

Terminal

% nano ~/.zshenv

6.2 /usr/libexec/java_home -v"{$Version}" to activate a specified JDK version.

Add the following content to activate the JDK 1.8

~/.zshenv

export JAVA_HOME=$(/usr/libexec/java_home -v1.8)

If we want JDK 14.

~/.zshenv

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

If we want JDK 15.

~/.zshenv

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

6.3 Source the file and print the $JAVA_HOME, done.

Terminal

% source ~/.zshenv

% echo $JAVA_HOME
/usr/local/Cellar/openjdk@8/1.8.0+275/libexec/openjdk.jdk/Contents/Home

References

51 comments on “How to Set $JAVA_HOME environment variable on macOS

  1. how to undo this? inspite of updatig the java_home. on running java -version, it still shows the modified one not the old one?
    PLease help

    Reply
  2. How to get this path ? I installed java 1.8
    /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

    Reply
  3.  % /usr/libexec/java_home 
    /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home

    when I type % /usr/libexec/java_home -V
    Matching Java Virtual Machines (2):
      1.8.202.08 (x86_64) “Oracle Corporation” – “Java” /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
      1.8.0_202 (x86_64) “Oracle Corporation” – “Java SE 8” /Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home
    /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home

    I tried to open my eclipse 2019-06 it says failed to create JVM

    please let me know how to fix it, thank you

    Reply
  4. Thank you for this article, useful to complete the Java setup required by Flutter on MacOS 11.

    Reply
  5. Hello, thank you this is super helpful. I am wondering if it is necessary to include the dollar sign and the parentheses when editing ~/.zshev. The only way I’ve gotten the correct output with echo $JAVA_HOME is by not including parenthesis or dollar sign when editing.

    Reply
  6. hey, after following up to 3. $JAVA_HOME and macOS 11 Big Sur for zsh, i get to 3.3 Source the file and print the $JAVA_HOME, done. – where it prints blank syntax. Any ideas?

    Reply
  7. on my system (macos 10.11.6) i have /usr/libexec/java_home too. not sure if perhaps openjdk put it there though.

    Reply
  8. Need this on my M1 Silicon Mac, the Zsh worked perfect. Thank you!

    Reply
  9. This was exactly as concise and explanatory (tough to optimize) as it could have been, thank you!

    Reply
  10. Hi,

    I am on a Mac running Catalina (10.15.7). I set the JAVA_HOME variable in the .zshrc file like you suggested on number 3. When I source the file I get the following error:

    zshrc:108: /Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home not found

    I know the file does exist. Any ideas on what is wrong?

    Reply
  11. Can anybody help me?, I just followed all the step in the post, but it doesn’t work to me.
    When I type: Echo $JAVA_HOME, only show me blank.
    Details:

    ⋊> ~ cat .bash_profile 22:42:12
    export JAVA_HOME=$(/usr/libexec/java_home)
    export M2_HOME=/Applications/apache-maven-3.6.1
    export PATH=$PATH:$M2_HOME/bin
    ⋊> ~ echo $JAVA_HOME 22:42:16

    ⋊> ~ java -version 22:42:43
    java version “1.8.0_211”
    Java(TM) SE Runtime Environment (build 1.8.0_211-b12)
    Java HotSpot(TM) 64-Bit Server VM (build 25.211-b12, mixed mode)
    ⋊> ~ javac -version 22:42:48
    javac 1.8.0_211
    ⋊> ~ source .bash_profile 22:42:51
    .bash_profile (line 1): $(…) is not supported. In fish, please use ‘(/usr/libexec/ja…)’.
    export JAVA_HOME=$(/usr/libexec/java_home)
    ^
    from sourcing file .bash_profile
    called on standard input

    source: Error while reading file ‘.bash_profile’

    Reply
    1. Due to recent Mac catalina update, by default terminal uses zsh. Zsh does not use .bash_profile. It uses ~/.zshrc file. Open up this file and add source ~/.bash_profile. Restart Terminal and it should work. Make sure your ~/.bash_profile file does not have source ~/.bash_profile line.

      Reply
      1. I have added JAVA_HOME in ~/.zshrc file and run source ~./bash_profile
        it does shows path in eco $JAVA_HOME command.

        But when I run elasticsearch it still shows :

        warning: usage of JAVA_HOME is deprecated, use ES_JAVA_HOME
        could not find java in JAVA_HOME at /Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/bin/java

        Reply
        1. source ~/.zshrc or restart the machine.

          Is this path exists?
          /Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/bin/java

          Reply
    2. export $PATH is optional.

      export $JAVA_HOME, and the macOS java can find the correct JDK.

      Reply
  12. Your site is one of the best out there for development. Thanks for what you provide.

    Reply
    1. Thanks, article is updated with more examples, and also the zsh shell.

      Reply
  13. Only your solution worked for me after unsuccessfully digging around the Internet for some time.
    Thanks a lot.

    Reply
  14. Works like a charm.. thanks a lot.. have been following your blog for quite sometime… kudos on your spot on and precise solutions…

    Reply
  15. Virus:bin root# java -version

    java version “1.8.0_25”

    Java(TM) SE Runtime Environment (build 1.8.0_25-b17)

    Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)

    Virus:bin root# whereis java|xargs ls -ltr

    lrwxr-xr-x 1 root wheel 73 Nov 25 14:35 /usr/bin/java -> /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java

    Virus:bin root#

    Reply
  16. To whom is interested to fix it up.

    1) whereis java | xargs rm -rf

    2) ln -s “/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java” /usr/bin/java

    3) java -version

    Reply
    1. export JAVA_HOME=”/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home”

      is probably a better solution that doesn’t require surgery to /usr/bin/java

      Reply
  17. Doesn’t work. Well it does but it sets JAVA_HOME only for specific terminal. When I open new tab in the terminal and write mvn –version it still shows 1.6 and echo $JAVA_HOME doesn’t show anything at all.

    Reply
    1. Make sure you are saving this in your bash profile. if you just type it into your terminal, it will not persist after you close the terminal.

      Reply
      1. I have a similar problem – I typed this into my ~/.bash_profile, but using the source command doesn’t persist in another shell. When I use $cat ~/.bash_profile it prints out the correct contents.

        Reply
        1. Did you try restarting Terminal altogether? Sometimes there is some weird funniness with session persistence. I always restart Terminal after editing .bash_profile just in case.

          Reply
    2. On older Mac OS X
      source ~/.bash_profile or restart the Terminal.

      On macOS 10.15 Catalina and later
      source ~/.zshenv or restart the Terminal.

      Reply
  18. Thanks a lot for these clear, precise and straightforward explanations.

    Reply
  19. Thank you! Another post on your blog that has helped me a lot.

    Reply
  20. Hi colleagues, its enormous article about educationand entirely explained, keep it up
    all the time.

    Reply

Leave a Comment

Your email address will not be published. Required fields are marked *