Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building with Gradle - Supplied javaHome must be a valid directory

Tags:

java

gradle

I am having a lot of trouble for what should be an easy task. I'm trying to build a simple Java project (just Hello World) with Gradle for one of my courses before classes begin. I cloned the repository, downloaded all the relevant software, and tried gradle build on the command line. I get the error Supplied javaHome must be a valid directory. You supplied: C:\Program Files\AdoptOpenJDK\jre-8.0.282.8-hotspot. This file that shows up was a previous version that I had removed.

I tried reinstalling everything, and I also checked the JAVA_HOME environment variable, which is set to C:\Program Files\AdoptOpenJDK\jdk-15.0.2.7-hotspot\. I've also relaunched the terminal and rebooted my computer. I'm using Git Bash as my shell, if that's anything relevant. Any advice on how to change the supplied javaHome?

Here are some relevant screenshots:

Result of gradle build

Result of java -version

JAVA_HOME environment variable

My (very sketchy) solution is to make a copy of the existing jre15 folder I have and rename it to jre-8.0.282.8-hotspot. I can build and run my program now, but it just seems very sketch.

One year update: I asked my prof when classes started when he said this solution was actually pretty ok and would suffice for the semester.

like image 699
p23he Avatar asked May 10 '21 03:05

p23he


People also ask

Why did my Gradle fail to complete?

I got the following message: Failed to complete Gradle execution. Cause: Supplied javaHome is not a valid folder. You supplied: C:\Program Files\Java\jdk1.7.0_45

Is javahome a valid folder in the JDK?

One of my coworkers updated their version of the JDK, and has been having nothing but issues. "Supplied javaHome is not a valid folder. You supplied: " We think we’ve changed the right locations. Where else can we look? It works from the command line using gradlew build, etc.

Is the Gradle Daemon able to launch successfully?

edited Expected Behavior Gradle Daemon is able to launch successfully. Current Behavior Gradle Daemon does not start and fails with the message Supplied javaHome must be a valid directory. You supplied: /usr/lib/jvm/zulu8-ca-amd64

What's going on with Gradle 7?

What appears to be happening with Gradle 7 is that when the build starts it attempts to check if an existing daemon can be used, in that check it appears to use the Java installation directory from a previous build to check if it matches what is requested.


Video Answer


2 Answers

I had a similar problem building a Java app on GitHub Actions. Once I upgraded from Java 11 to Java 16 I started to get the same error as you.

In my case it turned out to be a caching problem. I was caching two directories:

  1. ~/.gradle (on windows this should be something like C:\Users\username\.gradle)
  2. project/.gradle (this one is the gradle directory that is local to my app project).

Invalidating the caches fixed the issue for me.

like image 184
Miguel Ferreira Avatar answered Oct 09 '22 06:10

Miguel Ferreira


I had a similar problem after uninstalling a Java version with Gradle deamon running. Stopping the deamon fixed the issue for me:

gradle --stop

(Probably, a cross-platform equivalent of Miguel's solution)

like image 22
darw Avatar answered Oct 09 '22 08:10

darw