EGHDK
EGHDK

Reputation: 18130

JAVA_HOME is set to an invalid directory while running ./gradlew on OSX

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

Upvotes: 35

Views: 61488

Answers (7)

slimeball
slimeball

Reputation: 21

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

Upvotes: 2

3Agamy
3Agamy

Reputation: 11

Try this instead form Mac os

export JAVA_HOME="/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home"

Upvotes: 1

Scott Wyman Neagle
Scott Wyman Neagle

Reputation: 113

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}'))

Upvotes: -1

rahulrvp
rahulrvp

Reputation: 2126

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!

Upvotes: 13

stk1234
stk1234

Reputation: 1116

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

Upvotes: 21

gregwhitaker
gregwhitaker

Reputation: 13410

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

Upvotes: 1

lodlock
lodlock

Reputation: 3788

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

Upvotes: 87

Related Questions