./gradlew The operation couldn’t be completed. Unable to locate a Java Runtime
Asked Answered
M

2

28

When I run ./gradlew from project folder I get:

./gradlew
The operation couldn’t be completed. Unable to locate a Java Runtime.
Please visit http://www.java.com for information on installing Java.

The same with java:

which java
/usr/bin/java

/usr/bin/java
The operation couldn’t be completed. Unable to locate a Java Runtime.
Please visit http://www.java.com for information on installing Java.

I installed JDK in Android studio, seems it's installed in ~/Library/Android/sdk/sources/android-28

Setting export JAVA_HOME=/Users/mrgloom/Library/Android/sdk/sources/android-28/ or export JAVA_HOME=/Users/mrgloom/Library/Android/sdk produce error like:

ERROR: JAVA_HOME is set to an invalid directory: /Users/mrgloom/Library/Android/sdk/sources/android-28/

Please set the JAVA_HOME variable in your environment to match the
location of your Java installation.

How to specify java path for gradlew ?

Migdaliamigeon answered 9/8, 2021 at 10:42 Comment(4)
The android sdk nor the android sources are a JDKNauseous
Sounds to me like you should be using brew install openjdk@8Nauseous
Seems JDK is shipped with android studio: developer.android.com/studio/intro/studio-configMigdaliamigeon
It is, but this is meant to be used by the IDE, only, IMO. If you are expecting to individually run Gradle or Java commands outside the IDE, then install it outside of the IDENauseous
M
66

Using JDK shipped with Android Studio and setting export JAVA_HOME=/Applications/Android\ Studio.app/Contents/jre/Contents/Home helped me.

Migdaliamigeon answered 9/8, 2021 at 10:59 Comment(3)
In my bash profile, I had to include it with quotes instead of the backslash export JAVA_HOME="/Applications/Android Studio.app/Contents/jre/Contents/Home"Foozle
Thanks guys, helped for newly installed AS without the need to install Java.Frequency
For Android Studio Electric Eel and OSX Ventura 13.1 the AS JDK was in a different jbr folder: export JAVA_HOME="/Applications/Android Studio.app/Contents/jbr/Contents/Home"Allotment
I
2

Might need to clear caches and restart the studio. These steps worked for me -

  1. Set Java home. You can use embedded JDK in Android Studio.

export JAVA_HOME=/Applications/Android\ Studio.app/Contents/jre/Contents/Home.

Use Setting Java Home in MAC for reference if required.

  1. Close terminal
  2. Invalidate Caches & restart - Include 'Clear file system cache and local history'
Iowa answered 20/9, 2022 at 15:42 Comment(1)
Clearing caches and restarting the AS after setting up JAVA_HOME helped. Thanks!Teletype

© 2022 - 2024 — McMap. All rights reserved.