Explain Codes LogoExplain Codes Logo

How to set JAVA_HOME environment variable on Mac OS X 10.9?

java
environment-variables
mac-os
java-setup
Alex KataevbyAlex Kataev·Feb 21, 2025
TLDR

Set JAVA_HOME on Mac OS X 10.9 with:

export JAVA_HOME=$(/usr/libexec/java_home)

Insert this line in ~/.bash_profile for bash or ~/.zshrc for zsh. Apply the change with source ~/.bash_profile or source ~/.zshrc. To point to a specific Java version, use export JAVA_HOME=$(/usr/libexec/java_home -v 1.8), replacing 1.8 with your required version.

Step-by-step Guide to JAVA_HOME Setup

This section will give you a step-by-step guide to help you set up your JAVA_HOME correctly based on your specific needs and setups.

Ensure JDK Installation

Begin by ensuring you have a JDK installed; if not, download one from the Oracle website.

Set Up

Use the terminal to add the following to your ~/.bash_profile (for bash) or ~/.zshrc (for zsh):

export JAVA_HOME=$(/usr/libexec/java_home)

This code translates to "Hey JAVA_HOME, just point to whatever path /usr/libexec/java_home spews out." 😉

Choose Your Version

If you want to use a specific Java version, knock yourself out:

export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)

Replace 1.8 with your version number of choice.

Hit Refresh

For the terminal to reflect your recent changes without breaking a sweat by rebooting, use:

  • for bash:
    source ~/.bash_profile # Nice and refreshed, without the reboot circle of doom.
  • for zsh:
    source ~/.zshrc # Isn't it great when changes take effect instantly?

Test Your Setup

Take your new JAVA_HOME out for a spin and check:

echo $JAVA_HOME

Your terminal should now display the path to your designated JDK.

Tips, Tricks, and Common Hurdles

We've all been there - stuck at some point during setup. Here are some common issues and their solutions.

Recognize Your Shell

Are you using bash or zsh? Remember, the latest macOS versions use zsh by default. To know which one you're using, go ask your terminal: echo $SHELL. Then proceed accordingly.

Update Your Path

After setting JAVA_HOME, you may need to ensure your Java tools can be called directly from the terminal. Get it done with:

export PATH=$PATH:$JAVA_HOME/bin

Dealing with Multiple JDK Versions

Having multiple JDKs isn't rare. Specify your desired version in your JAVA_HOME as shown above. For managing multiple JDKs without losing your sanity, consider using relevant tools, like jEnv.

Overcome Profile Sourcing Issues

If your changes to .bash_profile or .zshrc don't reflect, check for correct permissions and if you've sourced the profile or logged back in. If still no luck, remember, Google is your best friend.

Testing Java Installation

Always, and this cannot be stressed enough, test your JDK installation:

java -version

When in Doubt, Check the Manuals!

The official Oracle guides and macOS resources for Java installations are always around to give you a hand. Homebrew can also simplify Java versions management.