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
🌐
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.
🌐
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 - ➜ ~ /usr/libexec/java_home -d 32 ➜ ~ /usr/libexec/java_home -d 64 · The tool can do some other stuff too and the manual page is clear and concise. Join Medium for free to get updates from this writer. ... This helps us because we can simply do something like this in our shell configuration files to make JAVA_HOME always point to the latest JDK by default.
🌐
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.
🌐
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.
🌐
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.
🌐
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.
Find elsewhere
🌐
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.
🌐
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.
🌐
Medium
kailash-pathak.medium.com › how-to-set-up-an-environment-variable-for-java-in-latest-mac-os-c14a8db3e59c
How to Set up an Environment variable for JAVA in Latest Mac Os - KailashPathak - Medium
January 5, 2021 - We cannot use ~/.bash_profile to export your environment variable since the bash shell is deprecated in the latest version of macOS. ... 1.Run /usr/libexec/java_home in your terminal and you will get things like /Library/Java/JavaVirtualMac...
🌐
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 - ... 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.
🌐
Apache Maven
maven.apache.org › install.html
Installation – Maven
1 week ago - You need a Java Development Kit (JDK) installed. Either set the JAVA_HOME environment variable to the path of your JDK installation or have the java executable on your PATH.
🌐
Scribd
scribd.com › document › 586996071 › javainMAC
Set JAVA_HOME on Mac OS X | PDF
JavaScript is disabled in your browser · Please enable JavaScript to proceed · A required part of this site couldn’t load. This may be due to a browser extension, network issues, or browser settings. Please check your connection, disable any ad blockers, or try using a different browser
🌐
Decoding the Web
decoding.wordpress.com › 2010 › 02 › 26 › how-to-set-the-java_home-variable-in-mac-os-x-snow-leopard
How to set the JAVA_HOME variable in Mac OS X – Snow Leopard | Decoding the Web
March 11, 2010 - ... The proper way to set JAVA_HOME on OS X is to export JAVA_HOME=`/usr/libexec/java_home`, because it respects the settings from the Java Preferences app, and works for non-Apple JVMs (eg.
🌐
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.
🌐
Snapcraft
snapcraft.io
Snapcraft - Snaps are universal Linux packages
name: freeplane title: Freeplane version: '1.8.1' summary: A free tool to structure and organise your information […] description: | Freeplane is a free and open source software […] base: core18 confinement: strict apps: freeplane: extensions: - gnome-3-28 command: freeplane-$SNAPCRAFT_PROJECT_VERSION/freeplane.sh environment: JAVA_HOME: $SNAP/usr/lib/jvm/java-11-openjdk-amd64 PATH: $JAVA_HOME/jre/bin:$PATH plugs: - home - network - cups-control parts: freeplane: plugin: gradle source: https://github.com/freeplane/freeplane.git source-tag: release-$SNAPCRAFT_PROJECT_VERSION gradle-version: '5.1.1' gradle-output-dir: BIN gradle-options: [binZip, -xtest, -xcreateGitTag] override-build: | snapcraftctl build unzip -o DIST/freeplane_bin-*.zip -d $SNAPCRAFT_PART_INSTALL/ build-packages: - unzip
🌐
Gradle
docs.gradle.org › current › userguide › installation.html
Installing Gradle
In File Explorer right-click on the This PC (or Computer) icon, then click Properties → Advanced System Settings → Environmental Variables. Under System Variables select Path, then click Edit. Add an entry for C:\Gradle\gradle-9.6.1\bin. Click OK to save.
🌐
GeeksforGeeks
geeksforgeeks.org › java › setting-environment-java
Setting up Environment Variables For Java - Complete Guide to Set JAVA_HOME - GeeksforGeeks
May 9, 2025 - Before setting up the environment ... set Java Environment Variables, you need to set, JAVA_HOME: It points to the directory where the JDK is installed on the system....