I recently purchased a new MacBook Pro, and this is my first Mac ever, so I’m still getting the hang of things. I’m also new to Java, as I was practicing on my Windows PC before it permanently died.
Now that I’ve installed my JDK, I need to set JAVA_HOME on my Mac, but I’m not sure how to do it. I tried following some guides like those from Mkyong and YouTube, but I ended up creating multiple files and getting error messages. For example, one message says another program might be editing the same file, and another suggests there was a crash while editing the file.
Can anyone guide me step-by-step on how to properly set JAVA_HOME on Mac?
If you’re using the Terminal and you’re still working with the Bash shell (which was default before macOS Catalina), you can set JAVA_HOME in your .bash_profile file.
Here’s how:
-
Open Terminal on your Mac.
-
Type nano ~/.bash_profile to open the .bash_profile file in the nano editor.
If the file doesn’t exist, it will create a new one.
-
Add this line to the file:
export JAVA_HOME=$(/usr/libexec/java_home)
This will automatically set the JAVA_HOME to the correct path for the JDK that’s installed.
-
Press Ctrl + X to exit, and then press Y to save the changes.
-
After that, run the following command to apply the changes:
source ~/.bash_profile
-
To verify if it worked, type:
echo $JAVA_HOME
This should return the path to your JDK.
Since macOS Catalina (10.15) and later uses Zsh as the default shell, you’ll need to modify the .zshrc file instead of .bash_profile. Here’s what to do:
-
Open Terminal.
-
Type nano ~/.zshrc to open the .zshrc file in the nano editor.
-
Add the following line to the file:
export JAVA_HOME=$(/usr/libexec/java_home)
-
Press Ctrl + X, then Y to save and exit.
-
Now, reload the .zshrc file by running:
source ~/.zshrc
-
Finally, verify the JAVA_HOME value:
echo $JAVA_HOME
It should show the path to your Java installation.
If you want to manually set JAVA_HOME instead of using the automatic /usr/libexec/java_home command, you can find the exact path of your JDK and set it explicitly.
-
Find the path to your JDK:
a. Run /usr/libexec/java_home -V in the terminal. This will list all the installed JDKs on your Mac.
b. Find the path to the JDK you want to use (it will look something like /Library/Java/JavaVirtualMachines/jdk-14.0.1.jdk/Contents/Home).
-
Open your shell configuration file (like .bash_profile or .zshrc, depending on your macOS version) by running:
nano ~/.zshrc # or nano ~/.bash_profile for older macOS versions
-
Add this line, replacing the path with the one you got in step 1:
export JAVA_HOME=“/Library/Java/JavaVirtualMachines/jdk-14.0.1.jdk/Contents/Home”
-
Save and exit by pressing Ctrl + X, then Y.
-
Reload the shell configuration file:
source ~/.zshrc # or source ~/.bash_profile
-
Verify with:
echo $JAVA_HOME
These should help you get JAVA_HOME set up on your Mac without issues. The first two methods are easier and more dynamic, while the third one gives you a manual approach, which could be useful if you want more control.
Let me know if you need further clarification or run into any issues!