"Unable to locate a Java Runtime" error when building for Android on Mac

The ‘run android’ command kept failing on my Mac (running Big Sur) with the following error:

Running Gradle build - failed!
[capacitor] [error] The operation couldn’t be completed. Unable to locate a Java Runtime.

I struggled for a while with it so just wanted to share what fixed it for me:

  • Find the SDK path in Android Studio (Android Studio > Preferences… > Build, Execution, Deployment > Build Tools > Gradle > Gradle JDK)
  • Set JAVA_HOME to that path in your .zshrc or .bashrc file. For example by adding the line:
    export JAVA_HOME=/Applications/Android\ Studio.app/Contents/jre/Contents/Home
  • Quit terminal, reopen and try running again

Hope this helps someone :slight_smile:


Thank you for posting it. You saved my time :slight_smile:

I had a similar issue with react native but I think installing Java (JRE) is creating a new problem while partly fixing the original. For whatever reason your computer can’t find your installed version of JDK. Either a path issue or another issue with the JDK install. This could potentially be fixed by simply updating your Java path in your terminals profile (.profile, .bash_profile, .zshrc, etc). i.e Make sure JAVA_HOME environment variable is set to the JDK’s folder. - if not reinstall JDK (instructions bellow)

When you install Java (JRE) and try build your app, the build now finds Javas JRE which has everything it is needs to run Java but not build it, this is why the new error is Could not find tools.jar… The JDK includes the compiler (javac) and tools (like javadoc and jdb) and is what is needed to build your app. You then fixed the second error by forcing gradle (build tool) to look in the correct folder for your JDK.

I would recommend fixing this issue fully by first removing the Java you installed by running the following in your terminal:

sudo rm -fr /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin
sudo rm -fr /Library/PreferencePanes/JavaControlPanel.prefPane
sudo rm -fr ~/Library/Application\ Support/Oracle/Java
And then reinstalling the JDK via brew by running: brew reinstall adoptopenjdk8

This may help you,
Rachel Gomez

The OP is not installing a JRE/JDK, is just configuring their JAVA_HOME to use the PATH of the JDK Android Studio bundles, so npx cap run android uses the same java version Android Studio uses when you run the app by using the run button.