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
Answer from Adrian Petrescu on Stack Overflow
🌐
Mac Install Guide
mac.install.guide › java › java-home
Set JAVA_HOME on Mac · Mac Install Guide · 2026
How to set JAVA_HOME on Mac. Find your JDK path, export JAVA_HOME in zsh, switch Java versions, and fix common macOS Java errors. Required for Maven, Gradle, and other tools.
🌐
Medium
zendesk.engineering › setting-java-home-on-mac-os-acd44e3a6027
Setting JAVA_HOME on Mac OS. This was originally posted in 2013 on… | by Barry Carroll | Zendesk Engineering
August 20, 2018 - I’m setting up a new computer, and I figured it was time to update it a bit. ... OS X has had a very useful utility, since 2003, that allows you to easily generate a path to a JDK. The simplest usage of the tool will return the default JDK, which in my case is update 131 of JDK 1.8. ➜ ~ /usr/libexec/java_home /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home
🌐
Medium
medium.com › @akhiltheguitarist › setting-up-java-path-on-macos-11d51e89a55f
Setting up java path on macOS. So recently, I changed my work machine… | by akhil | Medium
September 24, 2021 - Step 2: Assign the java home with the value. Pick the required java version to be set and execute the below command with change to the java version as required.
🌐
GitConnected
levelup.gitconnected.com › quick-way-to-set-java-home-in-mac-70cd4e82a9c5
Quick way to set JAVA_HOME in mac | by Vishnu | Level Up Coding
November 23, 2023 - If JAVA_HOME is not set, you need to install the Java Development Kit (JDK). Visit JDK Archive. Download the .tar.gz file appropriate for your Mac.
🌐
Scaler
scaler.com › home › topics › java › setting up java_home and the ide on mac os
Setting up JAVA_HOME and the IDE on MAC OS - Scaler Topics
July 14, 2024 - This steps helps in adding the java path to the zsh environment which is the default shell in MAC OS. We can save and exit nano. Step 7: Source the file and test using echo $JAVA_HOME · If the following output comes up, then we can be assured that we have successfully set up JAVA_HOME environment variable.
🌐
GitHub
gist.github.com › bf7aa20b007d8dda1ed2
How to Set $JAVA_HOME environment variable on Mac OS X · GitHub
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.
🌐
Baeldung
baeldung.com › home › java › set java_home variable in windows, mac os x, and linux
Set JAVA_HOME on Windows 7, 8, 10, Mac OS X, Linux | Baeldung
March 26, 2025 - Click Edit, and add %JAVA_HOME%\bin to the list of paths if it’s not already there. Click OK to apply the changes and restart the computer to ensure the changes take effect. Open Search and type advanced system settings.
Find elsewhere
🌐
Medium
medium.com › @bectorhimanshu › how-to-set-java-home-environment-variable-on-macos-x-10-9-or-later-versions-dd4fa6936899
How to Set JAVA_HOME environment variable on MacOS X 10.9 or later versions | by bectorhimanshu | Medium
March 15, 2024 - ... Launch the Terminal application, which can be found in the Utilities folder within the Applications directory or located using Spotlight. ... Use the export command in Terminal to set the JAVA_HOME environment variable.
🌐
TecAdmin
tecadmin.net › how-to-set-java_home-environment-variable-on-macos
How to Set JAVA_HOME environment variable on macOS – TecAdmin
April 26, 2025 - The /usr/libexec/java_home is the command line utility that returns the Path of the Java home directory from the current user’s settings. You can use this utility to set up the JAVA_HOME environment variable on your macOS.
🌐
Mkyong
mkyong.com › home › java › how to set $java_home environment variable on macos
How to Set $JAVA_HOME environment variable on macOS - Mkyong.com
January 19, 2021 - 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.
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › setup java home and ide on macos
How to set JAVA_HOME in MacOS? | upGrad
March 18, 2026 - Step-by-step guide on setting up Java home and an Integrated Development Environment (IDE) on macOS. Learn how to configure your Java development environment for seamless coding on your Mac system.
🌐
Java67
java67.com › 2015 › 11 › how-to-set-javahome-path-in-mac-os-x.html
How to set JAVA_HOME (PATH) in Mac OS? Example Tutorial | Java67
From version 10.6 onward, it's recommended to set JAVA_HOME variable using /usr/libexec/java)home and not using something like /System/Library/Frameworks/JavaVM.frameworks/Version/1.6.0_22/Home, which is subject to change from release to release. In order to set PATH just add this JAVA_HOME/bin into PATH. These steps work in OS X version Lion, Mountain Lion, Mavericks, Yosemite, and even in the beta version of the latest Mac version OS X El Capitan.
🌐
Apps Developer Blog
appsdeveloperblog.com › home › java › how to set java_home on macos
How to Set JAVA_HOME on macOS - Apps Developer Blog
April 20, 2020 - For example, open the terminal ... in the terminal window. ... To set the JAVA_HOME environment variable for a current user you will need to update the ~/.profile file....
🌐
Medium
medium.com › @bringetech › how-to-set-up-java-home-on-macos-a-clear-simple-guide-5033ddc3931c
🔧 How to Set Up JAVA_HOME on macOS (A Clear & Simple Guide) | by Bringe Raj | Medium
November 24, 2025 - This sets it only for the current terminal session. ... Everything should work smoothly. ... Perfect for developers using different JDKs across projects. Whenever Java stops working after an update, run /usr/libexec/java_home -V again—macOS may assign a new path.
🌐
CodeJava
codejava.net › java-core › set-java-home-in-macos-linux
How to set JAVA_HOME in macOS and Linux permanently
December 30, 2021 - How to set JAVA_HOME and update PATH environment variables in macOS/Linux permanently, with Z-shell and Bash shell
🌐
Medium
medium.com › notes-for-geeks › java-home-and-java-home-on-macos-f246cab643bd
java_home and JAVA_HOME on macOS. How to use java_home and JAVA_HOME with… | by Ross Kendle | Notes for Geeks | Medium
June 1, 2017 - $ /usr/libexec/java_home -v 1.7 --exec java -version java version "1.7.0_45" ... This setup is for bash, the Bourne Again Shell on macOS.
🌐
Groovy Post
groovypost.com › how-to › how to set java_home on a mac
How to Set JAVA_HOME on a Mac
September 18, 2023 - To set JAVA_HOME to this path, enter the following, replacing the path shown with the path returned in step 1 above: echo export "JAVA_HOME=/Library/Java/JavaVirtualMachines/microsoft-11.jdk/Contents/Home" ≫ ~/.zshrc ... You should see the correct path for your Java installation. If you’re using an older version of macOS, the default shell is likely to be bash.
🌐
Wikihow
wikihow.com › computers and electronics › software › programming › java › how to set java_home on windows, linux, & mac
How to Set JAVA_HOME on Windows, Linux, & Mac
March 5, 2025 - On Windows, you'll set JAVA_HOME in Advanced System Settings > Environment Variables. On a Mac, you'll need to add the Java path to your .zshrc file in a Terminal.