287

I cleaned the whole project by deleting local directories like ~/.gradle, ~/.m2 ~./android and ~/workspace/project/.gradle and chosing File -> Invalidate Caches / Restart... in Android Studio. Now 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

Needless to say, I deleted too much, the question is how can it be repaired again? Do you have any ideas how to fix this?

RaGe
  • 22,696
  • 11
  • 72
  • 104
SePröbläm
  • 5,142
  • 6
  • 31
  • 45
  • 11
    You should be able to run `gradle wrapper` again to regenerate the missing code... this is assuming you have no source control setup. – cjstehno Apr 22 '15 at 18:36
  • Do you have gradle installed on your machine? You can try invoking regular gradle instead of gradlew, should probably restore gradle wrapper as well. – RaGe Apr 22 '15 at 18:37
  • The funny thing is that I encountered this error when I changed my project folder name in Hindi. – Ashish Apr 10 '21 at 19:47
  • try use local gradle home instead of project it self – 袁文涛 Jun 07 '21 at 01:46

27 Answers27

300

In addition to @RaGe's answer may be the situation I faced where i had a global git ignore that was ignoring .jar files and so the gradle wrapper jar was never being committed. Thus I was getting that error on the Jenkins server after an attempted /var/lib/jenkins/my_project/gradlew build. I had to explicitly force an add of the jar and then commit:

git add -f gradle/wrapper/gradle-wrapper.jar
HankCa
  • 9,129
  • 8
  • 62
  • 83
  • 1
    a life saver: thank you! I had already upvoted so apparently I've been burned by this multiple times – Michael Easter Nov 04 '22 at 11:38
  • 1
    Although this answer has not been picked, this works for me. I did not aware that even I can run `./gradlew`, it does not mean that the necessary /gradle/wrapper/gradle-wrapper.jar exists (which as the answer suggested, it has been ignored by git). Adding it back solved the problem. – Ryan W Dec 12 '22 at 01:36
289

Your gradle wrapper is missing, broken or corrupted.

--
What is gradle wrapper:

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.

--
Restoring gradle wrapper:

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 in build.gradle:

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

Or in build.gradle.kts:

    tasks.register<Wrapper>("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)

--
Why was it missing in the first place?

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

RaGe
  • 22,696
  • 11
  • 72
  • 104
  • 13
    Another possible cause for this error is to have the gradle/wrapper/gradle-wrapper.jar file not executable, e.g. if you copied files from Windows... – centic Oct 21 '15 at 14:15
  • I had this case and eventually executing gradlew.bat with no arguments fixed it. Don't know why, but worth a try. – Alwyn Schoeman May 16 '17 at 23:49
  • Saved my day! I ignore gradle-wrapper.jar with "*.jar", so I build failed with same problem on pipeline. – Fan Applelin Nov 06 '20 at 02:15
  • 3
    I always appreciate an answer that includes a proper explanation of what's going on, not just a few lines to copy-paste to solve the problem. Thanks. – Mick Byrne Jan 27 '21 at 03:44
  • You nailed it RaGe. That is exactly what is happening in my case – Deepak Bandela Oct 13 '21 at 07:04
76

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

fahmy
  • 3,543
  • 31
  • 47
niklasingvar
  • 791
  • 5
  • 6
46

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.

Alex Palmer
  • 1,189
  • 9
  • 9
30

In my case, I left out wrapper sub folder while copying gradle folder and got the same error.

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

make sure you have the correct folder structure if you copy wrapper from other location.

├── build.gradle
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
└── settings.gradle
yantaq
  • 3,968
  • 2
  • 33
  • 34
  • Why would you copy wrapper? Simply executing `gradle wrapper` will give you the exact same result ;) – xeruf Sep 05 '18 at 21:04
  • @Xerus, in some corporate env there are network restrictions that you can not freely download packages. in this case you have to copy/move. – yantaq Sep 12 '18 at 17:42
12

I just copied gradle-wrapper.jar from one of my previous projects from path /android/gradle/wrapper and pasted into my app on the same path /android/gradle/wrapper and it worked perfect.

Chill Pill :)

Ali Akram
  • 4,803
  • 3
  • 29
  • 38
  • This is what fixed it, but I had a small difference, where I was using fastlane on GitHub action, and those files have been added to .gitignore in the same project.. so just remove them from the gitignore file and push them. – Remoo Aug 08 '23 at 09:09
10

You are probably missing gradle-wrapper.jar file under directory gradle/wrapper in your project.

You need to generate this file via this script in build.gradle file as below,

task wrapper(type: Wrapper) {
   gradleVersion = '2.0' // version required
}

and run task:

gradle wrapper

With gradle 2.4 (or higher) you can set up a wrapper without adding a dedicated task:

gradle wrapper --gradle-version 2.3

OR

gradle wrapper --gradle-distribution-url https://myEnterpriseRepository:7070/gradle/distributions/gradle-2.3-bin.zip

All the details can be found this link

Yuri
  • 4,254
  • 1
  • 29
  • 46
Luna Kong
  • 3,065
  • 25
  • 20
  • Lots of errors on windows 10 for "gradle wrapper --gradle-distribution-url https://myEnterpriseRepository:7070/gradle/distributions/gradle-2.3-bin.zip" – Love and peace - Joe Codeswell Jun 13 '18 at 15:50
  • Yes, the problem is missing the content of `gradle/wrapper` dir. This can be remedied by running the `gralde wrapper` as you stated. – Yuri Sep 27 '18 at 06:50
  • 1
    In case this command does not work, on AS there is Gradle panel on right side. Click `Gradle->ProjectName->ProjectName->Tasks->build setup->wrapper` – Jemshit May 15 '19 at 08:59
10

I followed the answers from above when i ran into this. And If you are having this issue than make sure to force push both jar and properties files. After these two, i stopped getting this issue.

git add -f gradle/wrapper/gradle-wrapper.jar
git add -f gradle/wrapper/gradle-wrapper.properties
LarsTech
  • 80,625
  • 14
  • 153
  • 225
Jose Peralez
  • 121
  • 1
  • 3
8

In my case (using windows 10) gradlew.bat has the following lines of code in:

set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%

The APP_HOME variable is essentially gradles root folder for the project, so, if this gets messed up in some way you are going to get:

Error: Could not find or load main class org.gradle.wrapper.GradleWrapperMain

For me, this had been messed up because my project folder structure had an ampersand (&) in it. Eg C:\Test&Dev\MyProject

So, gradel was trying to find the gradle-wrapper.jar file in a root folder of C:\Test (stripping off everything after and including the '&')

I found this by adding the following line below the set APP_HOME=%DIRNAME% line above. Then ran the bat file to see the result.

echo "%APP_HOME%"

There will be a few other 'special characters' that could break a path/directory.

Neil Watson
  • 2,801
  • 3
  • 18
  • 20
7

I saw the same error but in my case it was a fresh Git installation without LFS installed. The repo in question was set up with LFS and the gradle-wrapper.jar was in LFS so it only contained a pointer to the LFS server. The solution was simple, just run:

git lfs install

And a fresh clone did the trick. I suppose git lfs pull or just a git pull could have helped as well but the person with the problem decided to do a fresh clone instead.

cb2
  • 679
  • 9
  • 15
7

The problem statement is simple that its not able to locate the executable main class file.

If you are using gradle wrapper in your project, you should have strucutre as below

├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat

When you execute ./gradlew , it looks for the classpath and as per the code in gradlew or gradlew.bat, there is a line to add gradle-wrapper.jar to classpath

CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar

Now in my case there was an entry in the .gitignore as *.jar which excluded the gradle-wrapper.jar as well when the first commit was made.

So, to bring back the file, you have to execute below task

gradle wrapper

If you are not using latest gradle version then you have to have a wrapper task in build.gradle as below. This defines which gradle version will be bundled in wrapper. For latest versions of gradle, wrapper task is implicit.

task wrapper(type: Wrapper) {
    gradleVersion = '4.8' 
    //change this as per your project. Refer to distributionUrl in gradle-wrapper.properties to confirm the version
}

Once you execute the task, the gradle-wrapper-4.8.jar will be downloaded and dropped in the gradle/wrapper folder (as explained in above tree structure). Rename the file to gradle-wrapper.jar

However, as *.jar was excluded in the .gitignore file, I still cannot checkin it to store in github.

So add the below line in the .gitignore file to exclude gradle-wrapper.jar from ignored jars list due to *.jar.

!gradle-wrapper.jar

Git has actually shared sample .gitignore files for all programming languages.

Please refer for gradle andjava

Sanjay Bharwani
  • 3,317
  • 34
  • 31
5

you can also copy the gradlew.bat into you root folder and copy the gradlew-wrapper into gradlew folder.

that's work for me.

Guy
  • 133
  • 2
  • 11
3

I just had this in the OS X and solved as below:

$ gradle wrapper

Now, you can run the build command,

 $ ./gradlew build 
Arefe
  • 11,321
  • 18
  • 114
  • 168
3

If you are using MacOS and the ./gradle/wrapper/gradle-wrapper.jar file already exists but it still throws the same error, it might be due to MacOS java permissions on the Documents folder.

Go to System Preferences -> Security and Privacy -> Privacy Tab -> Files and Folders -> Java, and check the "Documents folder" checkbox (or any other that might appear where you are holding your apps).

I fought with this thing for hours so I hope it's useful for someone.

2

I fixed this problem with next fix (maybe it will help someone):

Just check if the parent folders of your project folder have names with spaces or other forbidden characters. If yes - remove it.

"C:\Users\someuser\Test Projects\testProj" - on this case "Test Projects" should be "TestProjects".

Ihor Khomiak
  • 1,151
  • 11
  • 17
1

@HankCa solved the problem in my case as well. I decided to change my dangerous **/*.jar ignores to self-explanatory ones like src/**/lib/*.jar to avoid such problems in the future. Ignores starting with **/* are a bit too hazardous, at least to me. And it's always a good idea to get the idea behind a .gitignore row just by looking at it.

1

In my case gradle-wrapper.jar got corrupted after replacing a bunch of files. Reverting to original one resolved the problem.

NeeK
  • 662
  • 1
  • 8
  • 21
  • 1
    Hey@Neek Welcome to Stack overflow please give answer on the new question this question have already more than 4 answer accepted so please concentrate on that part. – Dilip Dec 06 '17 at 09:59
  • 2
    Sure! all the above accepted answers did not help me so I put what helped me out. – NeeK Dec 08 '17 at 05:56
1

In my case , I had removed gradlew and gradle folders from project. Reran clean build tasks through "Run Gradle Task" from Gradle Projects window in intellij

enter image description here

Saurabh
  • 7,525
  • 4
  • 45
  • 46
1

On Gradle 5.x I use:

wrapper {
    gradleVersion = '5.5.1'
}
1

I have got this error because my app was in a folder that have an Arabic name and I solve it with just changing the Arabic folder name to an English one and it works fine.

So make sure that all the path of your app is written in English.

Mohamed Reda
  • 1,207
  • 13
  • 21
1

The global gitignore was ignoring all .jar and .properties file. Hence my gradle/wrapper was not added to the commit.

adding them back helped resolve the issue

Neel Alex
  • 605
  • 6
  • 10
  • This was exactly my problem as well; running `git add --force ./wrapper` and committing the results did the trick. – Tracy Logan May 08 '23 at 21:06
0

For Gradle version 5+, this command solved my issue :

gradle wrapper

https://docs.gradle.org/current/userguide/gradle_wrapper.html#sec:adding_wrapper

Raymond Chenon
  • 11,482
  • 15
  • 77
  • 110
0

I uninstalled gradle and reinstalled it and then created a new wrapper.

$ sudo apt remove gradle
$ sudo apt-get install gradle
$ gradle wrapper
Jonathan R
  • 3,652
  • 3
  • 22
  • 40
0

Our problem was that the gradle-wrapper.jar file kept getting corrupted by git.

We had to add a .gitattributes file with the line:

*.jar binary

Then remove the jar from git and add it again. Weirdly enough that was only required for one of our repos but not the others.

Oliver Metz
  • 2,712
  • 2
  • 20
  • 32
0
  1. from one of your any running project copy the folder of Gradle.
  2. replace it in your current project

then rebuild i hope you will be find working well.

sherkhan
  • 811
  • 8
  • 8
0

gradlew main

copy gradlew-wrapper.jar from other project in [your project]/gradle/wrapper

Deni Al Farizi
  • 1,547
  • 1
  • 8
  • 5
-1

It also might happen that you lost execution rights on the gradle folder, so

sudo chmod -R +x ./gradle

will help to fix the problem.

Martin Braun
  • 10,906
  • 9
  • 64
  • 105