12

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.

p23he
  • 121
  • 1
  • 6
  • On Ubuntu, I was messing with several parallel installations (one Stable, a recent Canary and an older one). No problems. Until I removed the older Canary version, that is. I started having the exact same issue. I have tried everything as well and I am quite clueless on how to overcome this issue as of now. If I manage to I will write here my findings as a comment as it probably would be a different solution for your case on Windows. – Guilherme Santos May 17 '21 at 17:30

4 Answers4

17

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.

Miguel Ferreira
  • 1,282
  • 11
  • 27
  • Had a similar problem in CircleCI. Cache invalidation worked. – Marat Asadurian Sep 26 '21 at 16:56
  • 2
    It was a caching problem for me, too. Specifically deleting the ~./.gradle directories `caches`, `daemon` and `wrapper` as per [this article](https://medium.com/back-market-engineering/put-your-android-studio-on-a-diet-fa4d364acb05#d129) did the trick. – Tom Ladek Feb 23 '22 at 07:22
9

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)

darw
  • 941
  • 12
  • 15
1

Update gradle(gradle-wrapper.properties) version from Android Studio to the lates version

enter image description here

0

For me i just deleted the .gradle in my folder C:\Users\username\.gradle. With Android Studio, dependencies are automatically reinstalled