how to solve Could not find or load main class org.gradle.wrapper.GradleWrapperMain

how to solve Could not find or load main class org.gradle.wrapper.GradleWrapperMain

Laxmi Queenie Author: Laxmi Queenie Date: 2022-08-11
how to solve Could not find or load main class org.gradle.wrapper.GradleWrapperMain

    1. FAILURE
    2. execution of the command ./gradlew leads to the following output:
      usr$ ./gradlew tasks
      Error: Could not find or load main class org.gradle.wrapper.GradleWrapperMain
      

    3. What is gradle wrapper
    4. gradlew is the gradle wrapper executable - batch script on windows and shell script elsewhere. The wrapper script when invoked, downloads the defined gradle version and executes it. By distributing the wrapper with your project, anyone can work with it without needing to install Gradle beforehand. Even better, users of the build are guaranteed to use the version of Gradle that the build was designed to work with.

    5. Restoring gradle wrapper
    6. It used to be that you needed to add a wrapper task to your build.gradle to restore gradle wrapper and all its dependencies. For instance:

      task wrapper(type: Wrapper) {
          gradleVersion = '4.1'
      }
      

      Newer versions of gradle do not require this. It is now a built-in task. Just run:

      gradle wrapper
      

      You can also supply additional flags to specify versions etc

      gradle wrapper --gradle-version 6.2 --distribution-type all
      

      When you run this task, a gradle wrapper script, and the required jar files are added to your source folders. Properties are stored in gradle/wrapper/gradle-wrapper.properties

      (You may need to install gradle locally to run this. brew install gradle on mac for instance. See more detailed instructions here)

    7. Why was it missing in the first place?
    8. OP seems to have deleted something that gradle wrapper depends on.

      But a common reason is that a .gitignore entry prevents wrapper jars from being checked into git. Note that the .gitignore in effect may be in the source folder, or a global one in your user home folder or git global configuration. It is common to have a *.jar entry in .gitignore.

      You can add an exception for gradlew's jar files in .gitignore

      *.jar
      !gradle/wrapper/gradle-wrapper.jar
      

      or force add the wrapper jar into git

      git add -f gradle/wrapper/gradle-wrapper.jar
      

      --
      Ref: Gradle Wrapper

    9. Other Solution
    10. What worked for me is to first run:

       gradle wrapper
      

      After successful build I was able to run

      ./gradlew assembleRelease
      

      Note: To be able run gradle wrapper first run brew install gradle. If installation successful run gradle wrapper from project root.

      Source and thanks: http://gradle.org/docs/current/userguide/gradle_wrapper.html and https://stackoverflow.com/users/745574/rage

      In my case it was a global .gitignore, as explained in @HankCa's answer.

      Instead of forcefully adding the jar, which you'll need to remember to do in each Gradle project, I added an override to re-include the wrapper jar in my global .gitignore:

      *.jar
      !gradle/wrapper/gradle-wrapper.jar
      

      This is useful to me as I have many projects that use Gradle; Git will now remind me to include the wrapper jar.

      This override will work so long as no directories above gradle-wrapper.jar (such as gradle and wrapper) are ignored -- git will not descend in to ignored directories for performance reasons.