How to fix the JAVA_HOME invalid directory error on a Mac


A step by step MacOS guide on fixing the ERROR: JAVA_HOME is set to an invalid directory in Android Studio.
The following tutorial aims to walk you through how to fix the error shown below.

Step One: Find the location of the Android Studio Java folder

In order to set the correct path, you must use Finder to find the location of the Java folder.
A | Go to Applications

Open Finder and go to Applications
B | Show Android Studio Package Contents

Right Click Android Studio and click Show Package Contents.
C | Locate the Java Home Folder

Navigate the Package Contents to find the location of the Java Home folder.
Past locations include:
- /Applications/Android\ Studio.app/Contents/jre/Contents/Home
- /Applications/Android\ Studio.app/Contents/jbr/Contents/Home
Step Two: Set JAVA_HOME

In Terminal, run the line below replacing PATH_TO_JAVA_HOME, with the path that you uncovered in Step One (i.e /Applications/Android\ Studio.app/Contents/jbr/Contents/Home).
export JAVA_HOME="PATH_TO_JAVA_HOME"
Please note that for changes to take effect you will have to restart Android Studio or run the line above within the Android Studio Terminal.