Apache Cordova Error: Failed to find ‘JAVA_HOME’ environment variable. Try setting it manually.

This blog post is brought to you by the developer of BitBudget. BitBudget is an automated budgeting app for Android and iOS which syncs with your bank account and helps you avoid overspending. If you’d like to quit living paycheck-to-paycheck and get a better handle on your finances, download it today! https://bitbudget.io

Hello there dear internet friend! It looks like you are having some trouble getting Apache Cordova to work. If you happen to be on a Mac, this blog post is for you! Likewise, a similar fix should work on Windows as well.

This isn’t my first time running into this quite annoying problem. Assuming you have Android Studio installed, the issue is either that Android Studio hasn’t set a global path to Java, or Android Studio is using a newer version of Java than the older version 8 that Cordova is still using.

Now if you happen to be really good with the UNIX operating system you can go track down Java and set the path yourself, or you can simply go with the easier solution that I’ve been using on all of my machines.

First, go ahead and install the brew package manager with the following terminal command:

$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" 

Next, install cask and the OpenJDK version of Java:

$ brew tap caskroom/cask

$ brew tap AdoptOpenJDK/openjdk

$ brew cask install adoptopenjdk8

Now, we still aren’t quite done. If you try to run cordova at this point you will likely encounter a similar error related to grade:

Checking Java JDK and Android SDK versions
ANDROID_SDK_ROOT=undefined (recommended setting)
ANDROID_HOME=/Users/topher/Library/Android/sdk (DEPRECATED)
Could not find an installed version of Gradle either in Android Studio,
or on your system to install the gradle wrapper. Please include gradle
in your path, or install Android Studio

To fix this, all you have left to do is install gradle with brew:

$ brew install gradle

And that’s it! Now you can run your Cordova app on your device:

$ cordova run android --device

 

topherPedersen