问题
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?
回答1:
gradlew
is the gradle wrapper executable - batch script on windows and shell script elsewhere. If you include the following lines in your build.gradle
,
task wrapper(type: Wrapper) {
gradleVersion = '4.1'
}
a gradle wrapper script is added to your source folders. 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.
In your deletions, you deleted something that gradlew depends upon. You can either pull just the gradlew files from your source repo, or if you have gradle installed run gradle wrapper
to restore it.
ref: Gradle Wrapper
回答2:
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
回答3:
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.
回答4:
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 runbrew install gradle
. If installation successful rungradle wrapper
from project root.
Source and thanks: http://gradle.org/docs/current/userguide/gradle_wrapper.html and https://stackoverflow.com/users/745574/rage
回答5:
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
回答6:
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
回答7:
you can also copy the gradlew.bat into you root folder and copy the gradlew-wrapper into gradlew folder.
that's work for me.
回答8:
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
回答9:
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.
回答10:
@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.
回答11:
In my case gradle-wrapper.jar got corrupted after replacing a bunch of files. Reverting to original one resolved the problem.
回答12:
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".
回答13:
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.
回答14:
On Gradle 5.x I use:
wrapper {
gradleVersion = '5.5.1'
}
回答15:
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
回答16:
For Gradle version 5+, this command solved my issue :
gradle wrapper
https://docs.gradle.org/current/userguide/gradle_wrapper.html#sec:adding_wrapper
回答17:
if it's a new project, remove existing folder and run $ npm install -g react-native-cli
check that runs without any error
来源:https://stackoverflow.com/questions/29805622/could-not-find-or-load-main-class-org-gradle-wrapper-gradlewrappermain