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
🌐
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 - 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.
🌐
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 - This tutorial will help you to set the JAVA_HOME environment variable on the latest older macOS operating systems. 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.
🌐
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.
🌐
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.
🌐
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
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 -V Matching Java Virtual Machines (2): 1.8.0_131, x86_64: "Java SE 8" 1.7.0_17, x86_64: "Java SE 7"
🌐
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
Just remember to use option -v to set JAVA_HOME to a particular version of JDB, of course, it's only useful if you have multiple JDK installed on your MacBook Pro. If you have installed Oracle JDK 1.6 then you can follow these steps to point ...
Find elsewhere
🌐
Medium
medium.com › developers-journal › how-to-set-java-home-environment-variable-on-mac-os-x-9cd799d80388
How to Set $JAVA_HOME environment variable on Mac OS X | by Vishal Saxena | Developers Journal | Medium
April 2, 2020 - How to Set $JAVA_HOME environment variable on Mac OS X In this journal entry we will see how we can setup the $JAVA_HOME environment variable on Mac OS X 10.5 or later versions. From OS X 10.5, Apple …
🌐
CodeJava
codejava.net › java-core › set-java-home-in-macos-linux
How to set JAVA_HOME in macOS and Linux permanently
December 30, 2021 - Z-shell (zsh) is the default shell in latest macOS operating systems. Firstly, you need to create a Z-shell resource file (.zshrc) in your user home directory, using the following command in terminal: cat > .zshrcThen type the following commands as content of the file: export JAVA_HOME=$HOME/jdk-17.0.1.jdk/Contents/Home · export PATH=$JAVA_HOME/bin:$PATHNote that the HOME environment variable refers to your user home directory.
🌐
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 OK to apply the changes and restart the computer to ensure the changes take effect. Open Search and type advanced system settings. In the shown options, select the View advanced system settings link.
🌐
Codemia
codemia.io › home › knowledge hub › how to set java_home environment variable on mac os x 10.9?
How to set JAVA_HOME environment variable on Mac OS X 10.9? | Codemia
January 26, 2025 - Before setting JAVA_HOME, verify the JDK is installed and visible. ... Typical output is a Contents/Home directory under /Library/Java/JavaVirtualMachines. Avoid hardcoding a single absolute JDK path when possible. On macOS, use:
🌐
W3Docs
w3docs.com › java
How to set JAVA_HOME environment variable on Mac OS X 10.9?
This command will output the path to the current JDK installation, such as /Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home.
🌐
Code2care
code2care.org › home page › java › setting java_home environment variable in macos mac os x
Setting $JAVA_HOME Environment Variable in macOS
Also if you want to run Java from the command line using the javac and java commands, you would need to set the Java home path in your environment variable. ... Save file file by pressing Control + X and typing Y to save. Apply the changes by excuting the command: source ~/.zshrc · To validate if JAVA_HOME is set or not, type command, % echo $JAVA_HOME /Library/Java/JavaVirtualMachines/zulu-11.jdk/Contents/Home % If you are using Bash shell: If you are using the Bash terminal then the above steps remain the same, just instead of .zshrc file you will use .bashrc file. To find your macOS (or Mac OS X) version you can make use of the command sw_vers, you will have one of these,
Top answer
1 of 5
19

in .bash_profile:

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.xx/Contents/Home
2 of 5
18

Adding the below answer to help those who are looking for step by step instructions on how to setup Java_Home on a Mac.

  1. Determine whether Java is installed by using the command below:

     which java
    

You will see something like this - /usr/bin/java

  1. Next Step will be to determine the version of Java Installed by using the command below:

     java -version
    

You will see something like java version "1.8.0_131"

  1. Next step will be to get the location where the Java is installed:

     cd /Library/Java
    
  2. Under the Java folder, select the folder with the version that was displayed earlier:

     /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home
    
  3. Next check if Java_Home is setup to the correct location:

     echo $JAVA_HOME
    

It will result in blank output if it is not already setup. If it returns the location, check if it points to the correct folder.

  1. You can add or update the Java_Home using the below commands:

     vi ~/.bash_profile
    

Navigate to the end of the file by pressing "Shift + g". Now press "i" to get to insert mode. Add the below lines in the bash_profile after replacing the path to the java home directory on your mac

    # Setting Java_Home
    export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home

Save this by pressing Esc followed by ":wq!"

  1. Finally open a new terminal window and test the variable is setup correctly:

     echo $JAVA_HOME
    

It should return the Java_Home path setup

🌐
Delft Stack
delftstack.com › home › howto › java › set java_home mac
How to Set JAVA_HOME on macOS | Delft Stack
February 2, 2024 - We must set JAVA_HOME to the actual path because the function /usr/libexec/java_home does not exist on earlier versions of macOS X. ... Insert the following lines in ~/.bash_profile. export JAVA_HOME=/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home · Now, source the file and print the JAVA_HOME. % source ~/.bash_profile % echo $JAVA_HOME /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home · The above commands will establish the JAVA_HOME environment variable for a single user. But what would happen if you wanted this variable to be set globally for every user using the system?
🌐
YouTube
youtube.com › watch
How to Set JAVA_HOME Environment Variable on macOS Using ZSH (M1 | M2 | M3) - YouTube
By the end of this tutorial, you’ll have successfully set the `JAVA_HOME` environment variable on macOS using the ZSH shell, making it easier to compile and ...
Published   September 28, 2024
🌐
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 - java_home is the best way on macOS to execute a Java command ...jdk/Contents/Home/bin/<command> for a specific version of the JDK. And it is independent of the setting of JAVA_HOME. ... $ /usr/libexec/java_home --exec java -version java version "1.8.0_121" Java(TM) SE Runtime Environment (build 1.8.0_121-b13) Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)