/errors/gradle/could-not-find-or-load-main-class-gradlewrappermain
Gradle: Error: Could not find or load main class org.gradle.wrapper.GradleWrapperMain
- tool
- Gradle
- exit code
- 1
- versions
- Gradle 4 – 9
- os
- any
Error: Could not find or load main class org.gradle.wrapper.GradleWrapperMain means ./gradlew started Java with gradle/wrapper/gradle-wrapper.jar on the classpath, but that jar is missing or unreadable, so the wrapper's main class cannot load. The gradlew script is only a launcher; the jar contains the code that downloads and starts Gradle. Commit gradle/wrapper/gradle-wrapper.jar (it is often excluded by a *.jar rule in .gitignore) or regenerate it with gradle wrapper.
Reproduce it
The real gradlew script from Gradle 8.10, without the jar next to it:
curl -sfL https://raw.githubusercontent.com/gradle/gradle/v8.10.0/gradlew -o gradlew
sh gradlew --versionOutput · exit 1
Error: Could not find or load main class org.gradle.wrapper.GradleWrapperMain Caused by: java.lang.ClassNotFoundException: org.gradle.wrapper.GradleWrapperMain
Java adds Caused by: java.lang.ClassNotFoundException: org.gradle.wrapper.GradleWrapperMain on the next line.
Newer wrappers: Unable to access jarfile
Wrapper scripts generated by recent Gradle versions start the jar with java -jar instead of a main class, so the same problem prints a different message:
curl -sfL https://raw.githubusercontent.com/gradle/gradle/v9.0.0/gradlew -o gradlew
sh gradlew --versionOutput · exit 1
Error: Unable to access jarfile /tmp/work/gradle/wrapper/gradle-wrapper.jar
Both messages have the same cause and the same fix.
Fix
With Gradle installed on the machine, regenerate the wrapper files in the project root and commit them:
gradle wrapper --gradle-version 9.0.0 # writes gradlew, gradlew.bat and gradle/wrapper/*
git add -f gradle/wrapper/gradle-wrapper.jar gradle/wrapper/gradle-wrapper.properties gradlew gradlew.bat
git commit -m "Add Gradle wrapper"If .gitignore excludes jars, add an exception so the wrapper stays tracked:
*.jar
!gradle/wrapper/gradle-wrapper.jar