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.
Step 1. Be sure you don’t have the JAVA_HOME already set
Run this command:
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/
Step 3. Add the JAVA_HOME to the Bash configuration file
Let’s open the file (if don’t exist, just create it):
Add the JAVA_HOME variable at the end of ~/.bash_profile
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:
This command should now print the correct JAVA_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.