After fixing permission error on gradlew https://spacetech.dk/error-gradle-script-home-runner-work-gradlew-is-not-executable.html We ran into the next issue where the error message is a bit vague in regards to Gradle setup.
Error: Could not find or load main class org.gradle.wrapper.GradleWrapperMain Caused by: java.lang.ClassNotFoundException: org.gradle.wrapper.GradleWrapperMain Error: Gradle build failed: process exited with status 1
But taking a closer look at the CI pipeline action for Gradle there is not specified a version. So what version is taking? Why are we getting could not load main class org.gradle.wrapper.GradleWrapperMain.
- name: Build with Gradle uses: gradle/gradle-build-action@v2 with: arguments: build
Looking at the documentation at https://github.com/gradle/gradle-build-action#use-a-specific-gradle-version we see the default is ‘current’. Alright, so what’s the current version. Let us go one step further. At https://gradle.org/install/ we can locate the current version, as of this writing the current Gradle release is 7.4.2.
Next step is to figure out what version is my current gradle project actually running on. Take a look at \gradle\wrapper\gradle-wrapper.properties in your root folder.
distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists distributionUrl=https\://services.gradle.org/distributions/gradle-6.8-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists
Looking at the third line we can identify the Gradle version is in fact not version 7.4.2 but rather 6.8.
Solution for Gradle Wrapper Main
Now for the solution. We have identified the Gradle version should be 6.8. So let’s add that to the CI pipeline for the Gradle action by appending gradle-version as stated in the documentation. https://github.com/gradle/gradle-build-action#use-a-specific-gradle-version
- name: Build with Gradle uses: gradle/gradle-build-action@v2 with: arguments: build gradle-version: 6.8
Doing a new run with the change and the build goes through. We can see the provision Gradle 6.8 now and able to download.
Provision Gradle 6.8 Gradle distribution 6.8 not found in cache. Will download.
In the end we get a green build icon. Hurray!
The error “Could not find or load main class org.gradle.wrapper.GradleWrapperMain” typically occurs when Gradle is unable to locate or load the Gradle wrapper script or the required dependencies.
Here are some possible solutions you can try:
- Check that the Gradle wrapper files are present in your project directory. These files should include a “gradlew” script and a “gradle” folder that contains the required Gradle distribution.
- Check that the Gradle wrapper script has executable permissions. If not, you can add them by running the command “chmod +x gradlew” in your project directory.
- Check that you have the correct version of Gradle installed on your system. You can check the required version by looking at the “gradle-wrapper.properties” file in your project directory. If you don’t have the correct version installed, you can download it from the Gradle website or use a tool like SDKMAN to manage multiple Gradle versions.
- Try deleting the “gradle” folder and the “gradlew” script in your project directory, and then running the command “gradle wrapper” to regenerate them.
- Check that your classpath and environment variables are set up correctly. Gradle requires that the JAVA_HOME environment variable is set to the location of your Java installation, and that the “bin” directory containing the “java” executable is included in your PATH.
- Check if the Gradle wrapper files are present in your project directory. The Gradle wrapper files include gradle-wrapper.properties, gradlew, and gradlew.bat. If these files are missing, you can generate them by running the “gradle wrapper” command in your project directory.
- Make sure that the Gradle version specified in the gradle-wrapper.properties file matches the Gradle version you have installed on your system. If the versions don’t match, you can either update your Gradle installation to match the version specified in the gradle-wrapper.properties file, or update the version in the gradle-wrapper.properties file to match your installed Gradle version.
Hopefully, one of these solutions will help you resolve the error and allow you to run Gradle successfully.