'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:
Navigated to
/Library/Java/JavaVirtualMachines.jdk1.8.0_11.jdkexists, but so doesjdk1.7.0_79.jdkwhich javaprints out/usr/bin/javaprintenvprints
...
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home JDK_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home
...
javac -versionprintsjavac 1.8.0_11which javacprints/usr/bin/javac
Solution 1:[1]
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
Solution 2:[2]
For me, I got that error no matter what I tried. Deleting the JAVA_HOME var worked for me.
Solution 3:[3]
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.

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

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!
Solution 4:[4]
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
Solution 5:[5]
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}'))
Solution 6:[6]
Try this instead form Mac os
export JAVA_HOME="/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | lodlock |
| Solution 2 | stk1234 |
| Solution 3 | rahulrvp |
| Solution 4 | |
| Solution 5 | Scott Wyman Neagle |
| Solution 6 | 3Agamy |
