How to set JAVA_HOME in Mac OS X High Sierra

How to set JAVA_HOME in Mac OS X High Sierra

This post describes how to configure the JAVA_HOME environment variable required by many enterprise tools, frameworks, and CMSes. For instance, Liferay, Tomcat, etc. will use this variable to work properly.

In case you have changed your default shell (Bash) that comes with OS X out-of-the-box, please read How to set JAVA_HOME in Mac OS X High Sierra with Fish shell

Step 1. Be sure you don’t have the JAVA_HOME already set

Run this command:

echo $JAVA_HOME

If the output is empty, then we don’t have the variable correctly set.

Step 2. Find the Java Development Kit directory

If you don’t know where the Java Development Kit (JDK) was installed, just run:

sudo find / -name javac

The output may be similar to this:

/usr/bin/javac
/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/bin/javac
/System/Library/Frameworks/JavaVM.framework/Versions/A/Commands/javac
find: /private/var/db/ConfigurationProfiles/Store: Operation not permitted
find: /private/var/folders/y5/abbasdf/0/SafariFamily: Operation not permitted

Notice the second line, that is our JDK installation directory, so the directory we need to use is this one: /Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/

The path may vary depending on the exact version of Java you have installed

Step 3. Add the JAVA_HOME to the Bash configuration file

Let’s open the file (if don’t exist, just create it):

nano ~/.bash_profile

Add the JAVA_HOME variable at the end of ~/.bash_profile

export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/"

Save the file ~/.bash_profile (in nano just press Control + O followed by Control + X to close the file). Finally, let’s review or configuration by typing:

echo $JAVA_HOME

This command should now print the correct JAVA_HOME:

/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/

If you don’t see your environment variable, please close all your terminal windows and re-start them so you have a “fresh” start.

Sweet, isn’t it?

See you next time.

Alex Arriaga

  • Prev Post
  • Next Post