Here I’ll explain how to add JAVA_HOME variable on your MacBook (Mac OS).
Set JAVA_HOME in .bash_profile
Open Launchpad and search for Terminal and launch it.
Edit ~/.bash_profile:
nano ~/.bash_profile
and add to the end:
export JAVA_HOME=$(/usr/libexec/java_home)
${/usr/libexec/java_home} is a symlink for current Java.
Press Ctrl+X to exit and “Y” to save your changes.
Now we need to recompile .bash_profile to apply changes:
source .bash_profile
Mac OS also allows you to edit ~/.profile instead of ~/.bash_profile.
How to check if JAVA_HOME on Mac?
Type “echo $JAVA_HOME” command to check Java Home environment variable on Mac OS X.
Success result should look like this:
/Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home