32

I tried running ./gradlew from an Android project directory, but I get an error of:

ERROR: JAVA_HOME is set to an invalid directory: /Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home

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

Things I've tried:

  1. Navigated to /Library/Java/JavaVirtualMachines. jdk1.8.0_11.jdk exists, but so does jdk1.7.0_79.jdk

  2. which java prints out /usr/bin/java

  3. printenv prints

...

JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home JDK_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home

...

  1. javac -version prints javac 1.8.0_11

  2. which javac prints /usr/bin/javac

EGHDK
  • 17,818
  • 45
  • 129
  • 204

7 Answers7

81

Check if /usr/libexec/java_home exists. If it does then try running

export JAVA_HOME=`/usr/libexec/java_home`

and rerunning your gradlew build. If it works then make it permanent with

echo export "JAVA_HOME=\$(/usr/libexec/java_home)" >> ~/.bash_profile
lodlock
  • 3,638
  • 1
  • 19
  • 15
21

For me, I got that error no matter what I tried. Deleting the JAVA_HOME var worked for me.

stk1234
  • 1,036
  • 3
  • 12
  • 29
  • 4
    Yup, perfectly worked fine! I notice the gradle already init the JAVA_HOME inside project directory (in my case, i used flutter), so I deleted the JAVA_HOME in .zhrc – Ade Firman Fauzi Nov 07 '20 at 17:17
13

If you see this message in 2021 after upgrading to Android Studio Arctic Fox (2020.3.1) Stable, then the following answer should help to get yourself unblocked.

Open the "Project Structure" window from the "File" menu and you can see that the JDK location is now moved to the Gradle settings. enter image description here

Now, click on the Gradle Settings link and you can see another window in which the current JDK location is specified. enter image description here

Now you should edit your ~/.bashrc OR ~/.zshrc to update the value of JAVA_HOME env variable.

That's it!

Now run the source ~/.bashrc OR source ~/.zshrc command or restart your terminal and enjoy running the ./gradlew command in your project.

Cheers!

rahulrvp
  • 2,006
  • 1
  • 19
  • 26
1

I have added the following to my .bash_profile to help sort out issues such as this one. This has the added benefit of being able to run setjdk {version} and switch java versions on the fly.

function setjdk() {
  if [ $# -ne 0 ]; then
   removeFromPath '/System/Library/Frameworks/JavaVM.framework/Home/bin'
   if [ -n "${JAVA_HOME+x}" ]; then
    removeFromPath $JAVA_HOME
   fi
   export JAVA_HOME=`/usr/libexec/java_home -v $@`
   export PATH=$JAVA_HOME/bin:$PATH
  fi
}

function removeFromPath() {
  export PATH=$(echo $PATH | sed -E -e "s;:$1;;" -e "s;$1:?;;")
}

#Default JDK to Java 8
setjdk 1.8
gregwhitaker
  • 13,124
  • 7
  • 69
  • 78
1

Try this instead form Mac os

export JAVA_HOME="/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home"
3Agamy
  • 11
  • 3
  • This does not look like JDK location, but Java plugin for Web location – Lubos Horacek Feb 01 '22 at 13:24
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Lubos Horacek Feb 01 '22 at 13:25
1

In macOS, the JDK installation path is/Library/Java/JavaVirtualMachines/jdk-10.jdk/Contents/Home. See the official documentation.

Adriaan
  • 17,741
  • 7
  • 42
  • 75
slimeball
  • 11
  • 1
-1

If anyone comes here using fish, adding the following line to ~/.config/fish/config.fish solves the problem for me.

set JAVA_HOME (/usr/libexec/java_home -v (java --version | awk 'NR==1{print $2}'))