If the gradle wrapper for your project is broken, you can reinstall it with the “gradle wrapper” command. For example, below is a common error if you have cloned a git repository where the owner has not pushed all the necessary gradle components into the repo.
$ ./gradlew --info Error: Could not find or load main class org.gradle.wrapper.GradleWrapperMain
This can be fixed by running the “gradle wrapper” command, but this requires that you have gradle installed first.
Install Gradle
# java required java --version # download the latest gradle zip using your browser # https://gradle.org/releases/ # extract downloaded version unzip gradle-7.5.1-bin.zip # add to PATH export PATH=$PATH:$(realpath gradle-7.5.1/bin) # smoke test gradle --version
Fix Gradle wrapper
Fix the gradlew wrapper for the project by changing directory into the project directory and running the “gradle wrapper” task.
# fix gradle wrapper gradle wrapper # smoke test gradle wrapper $ ./gradlew --version $ ./gradlew tasks
Upgrade Gradle wrapper
If you want to specify an exact version of gradle to use, you can specify a flag.
# upgrade gradle wrapper gradle wrapper --gradle-version=7.4 # check version ./gradlew --version
Recovering when gradle version and .class files are not compatible with JDK
If the existing gradlew version is not compatible with JDK in use on your system, you may see errors like below. For example, a project originally built gradle 7.x/Java 17, and now trying to be run on a system with JDK25.
$ ./gradlew --version ... Could not open settings generic class cache for settings file ... > BUG! exception in phase 'semantic analysis' in source unit '_BuildScript_' Unsupported class file major version 69
See the compatibility matrix for gradle, the fix is to manually update the gradle distribution being used.
$ cat gradle/wrapper/gradle-wrapper.properties | grep distribution distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip
As an example, moving this property from 7.5.1 to 8.14.4, should then allow you to run “./gradlew –version”.
REFERENCES
stackoverflow, explicitly setting gradle version
Gradle forums, print java version being used
NOTES
Fixing “Cannot lock execution history cache (…) as it has already been locked by this process”
find ~/.gradle -type f -name "*.lock" -delete
Showing the gradle and java version used, from build.gradle
println GradleVersion.current().toString() println org.gradle.internal.jvm.Jvm.current()
Specifying JVM to use in gradle.properties
org.gradle.java.home=/opt/homebrew/opt/openjdk@21