Are you tired of encountering the dreaded “JAVA_HOME is not set and no ‘java’ command could be found in your PATH” error when building your Unity project with the Google AdMob (or other) library included? Look no further! After extensive testing and research, I have found two simple solutions to this frustrating issue.
When you install Android build support in Unity, it will install a specific version of the Java Open JDK and the Android platform tools within the Unity installation folder. If you receive a build error stating that the JAVA_HOME variable is not set, and the java command cannot be found, then the configuration linking to this installation is broken. This is not often an indication that you need to install Java, and installing a different version of Java won’t work – Unity wants a specific version.
Option 1: A Unity bug?
I stumbled upon a supposed bug in Unity where the “JDK installed with Unity (recommended)” option is selected by default but ignored. To fix this, simply click the “Copy Path” button, uncheck “JDK installed with Unity” within the External Tools options in Preferences, and then paste the path back in. Voila! Successful builds… hopefully! For me this option worked until I next restarted Unity.
Option 2: Environmental variables to the rescue!
If option 1 doesn’t work for you, another solution is to create a new environmental variable called “JAVA_HOME” and populate it with the JDK path for the version of the JDK installed with Unity. To do this, copy the path from External Tools and paste it into a new environmental variable. Then, follow these steps:
Click Start and search for “system environment variables” and select the option that appears.
Click on the Environment Variables button and then click “New” under “System Variables”.
Enter the “Variable Name” as “JAVA_HOME” and the “Variable Value” as the JDK path you copied from External Tools.
Click OK on all of the open windows.
With these easy steps, you’ll be able to build your game successfully and get back to designing and developing. Say goodbye to the “JAVA_HOME is not set and no ‘java’ command could be found in your PATH” error for good!