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 - Quick way to set JAVA_HOME in mac Okay — got new mac and playing around Java project. Now first thing is to set up my JAVA_HOME variable. Thought to write a post so that it’s helpful to others …
🌐
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 - export JAVA_HOME=”/usr/libexec/java_home -v 1.6.0_65-b14–462 ... The appropriate java version would be displayed in the terminal. ... Press Ctrl+X to exit. Press Y to save the changes and press ENTER.
🌐
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 - In the System Properties window, select the Path variable under the System Variables section. Click Edit, and add %JAVA_HOME%\bin to the list of paths if it’s not already there.
🌐
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
🌐
Chief Delphi
chiefdelphi.com › technical › programming
Setting VS Code java.home (Mac) - Programming - Chief Delphi
January 7, 2019 - I was following the ScreenSteps guide for installing VS code when I ran into a discrepancy. My VS code: Screensteps example: Running the command results in this error: Any ideas on what is going on? I could not find any references to setting the value manually.
🌐
Umich
em8e.eecs.umich.edu › jws › mac_instructions_figs › mac_java_instructions.pdf pdf
Enabling Java Web Start Applications for the Mac
You only need to do this once. 2.1 Go to 'System Preferences', it is in the menu available from the apple icon on the · upper-left part of your screen. 'System Preferences' is usually the second option in the · pull-down menu. The screen will look like this: Firefox · file:///home/lep/...
🌐
Oracle
java.com › en › download › help › mac_install.html
How do I install Java for my Mac?
The screen shots and instructions below are for Java 8 Update 311 (8u311). If you are installing another version, make sure you change the version number appropriately. Example: For Java 8 Update 311 (8u311) the file to download is jre-8u311-macosx-x64.dmg
🌐
W3Schools
w3schools.com › java › java_getstarted.asp
Java Getting Started
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
🌐
GitHub
github.com › swagger-api › swagger-codegen › issues › 11908
Build fails on Mac, complaining about JAVA_HOME ...
September 23, 2022 - I even tried setting JAVA_HOME to point directly to /Library/Java/JavaVirtualMachines/openjdk.jdk, but Maven still gives the same error.
Author   swagger-api
🌐
Scaler
scaler.com › home › topics › how to set java_home?
How to set JAVA_HOME? - Scaler Topics
May 4, 2023 - Apple has introduced a command line tool (/usr/libexec/java_home) that automatically looks for the most suitable Java version specified in Java Preferences for a given user. Example-1 ... Once you have saved the file, open a terminal window and run the given source command to apply the changes:
🌐
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. On Linux, you can add the Java path to your .bashrc.
🌐
Delasign
delasign.com › blog › android-studio-fix-java-home-invalid-directory
How to fix the JAVA_HOME invalid directory error on a Mac
June 7, 2023 - In Terminal, run the line below replacing PATH_TO_JAVA_HOME, with the path that you uncovered in Step One (i.e /Applications/Android\ Studio.app/Contents/jbr/Contents/Home). ... Please note that for changes to take effect you will have to restart Android Studio or run the line above within ...
🌐
Qlik Talend Help
help.qlik.com › talend installation and upgrade guide › talend cloud installation prerequisites › setting up java_home
Setting up JAVA_HOME | Talend Installation and Upgrade Guide Help
In the directory where Java is installed, find the location of the bin directory that contains the virtual machine: bin\server\jvm.dll. ... Open the Start menu and type Environment variable in the search bar to open the Environment variable properties. Click Environment Variables....
🌐
Medium
medium.com › @aniketvishal › how-to-install-java-and-setup-java-home-path-in-macos-apple-silicon-m1-m2-2edf185b992c
How to install Java 21 and setup JAVA_HOME Path in macOS 🍏 (Apple Silicon) | by Aniket Vishal | Medium
October 22, 2024 - The Environment file should be ... of your mac and it’s name should be “.zshenv”. Use the below code to create your zShell Environment file. ... Use the following code to open the zShell Environment file. ... Add the below path in your zShell Environment file and save it. ... Enter the following code in the terminal after you saved and closed the environment file to apply the changes and refresh your profile. ... Your JAVA_HOME path is set ...