— macOS, Java, Environment Variables, Terminal, Development — 1 min read
If you're a Java developer on macOS, you've probably run into JAVA_HOME issues at some point. Whether you're setting up a new development environment or juggling multiple Java versions, getting your environment variables right is crucial. Let's walk through the process of checking and setting up JAVA_HOME properly.
First things first - let's see what your system thinks JAVA_HOME is:
1echo $JAVA_HOME
Want to know which Java version you're actually using? Try:
1java -version
On macOS, finding your Java installation is straightforward. To see all installed Java versions:
1/usr/libexec/java_home -V
This handy command shows you every Java version installed on your Mac.
Sometimes you need to switch Java versions just for a quick test or specific project. Here's how to do it for your current terminal session:
1export JAVA_HOME=$(/usr/libexec/java_home)
Need a specific version? No problem:
1export JAVA_HOME=$(/usr/libexec/java_home -v "17") # Replace 17 with your version
For a lasting setup, you'll want to add JAVA_HOME to your shell's configuration:
1# For Zsh (default in newer macOS)2nano ~/.zshrc3
4# For Bash5nano ~/.bash_profile
1export JAVA_HOME=$(/usr/libexec/java_home)
1source ~/.zshrc # or source ~/.bash_profile for Bash
Let's make sure everything's working as expected:
1echo $JAVA_HOME2java -version3javac -version
If things aren't working quite right, here are some quick checks:
1which java
1ls -l $JAVA_HOME
1ps -p $$ | grep -o "[^/]*$"
echo $JAVA_HOME
Whether you're working on legacy projects or cutting-edge applications, proper JAVA_HOME configuration makes life much easier. Save yourself future headaches by taking the time to set it up correctly now. And remember - if you're switching between different Java projects often, a version manager might be worth looking into.