share
Stack OverflowCould not find or load main class org.apache.maven.wrapper.MavenWrapperMain
[+71] [8] hsluoyz
[2018-04-30 16:01:53]
[ java spring maven spring-boot travis-ci ]
[ https://stackoverflow.com/questions/50104172/could-not-find-or-load-main-class-org-apache-maven-wrapper-mavenwrappermain ]

I have a spring boot project here: https://github.com/jcasbin/jcasbin-springboot-plugin. I encountered the following error in Travis CI [1]:

shell
3.43s$ ./mvnw install -DskipTests=true -Dmaven.javadoc.skip=true -B -V
/home/travis/build/jcasbin/jcasbin-springboot-plugin
Picked up _JAVA_OPTIONS: -Xmx2048m -Xms512m
Error: Could not find or load main class org.apache.maven.wrapper.MavenWrapperMain
The command "eval ./mvnw install -DskipTests=true -Dmaven.javadoc.skip=true -B -V " failed. Retrying, 2 of 3.

It seems that the mvnw command fails. This file is generated by my IDE: IntelliJ IDEA 2018.1. I don't know what it is used for and why does it fail?

I already fired an issue in Spring Boot's github issues here [2], but Spring project said it's Maven wrapper's issue and pointed me here. I don't quite understand what it means. Is it a Maven bug? How to fix the error then?

Maybe take a look at How to create a Minimal, Complete, and Verifiable example... I believe nobody is willing to read and understand your code from zero - Constantin Müller
This isn't the error, you need to look at the maven output log for "mvn install". - Nyamiou The Galeanthrope
Possible duplicate of What does "Could not find or load main class" mean? - hsluoyz
[+130] [2018-10-11 00:09:04] Oleksii Volynskyi

Phil's answer is correct. And here is how to create .mvn directory with needed jar inside.
I found the answer here (https://www.baeldung.com/maven-wrapper)

If you have maven and you want to make maven wrapper working, you need to setup maven wrapper:

mvn -N io.takari:maven:wrapper

It should create .mvn directory and put needed jar in it.


(10) Although @PhilWebb's answer maybe right, it addressed the diagnosis but not the remedy. This helped, although what is takari and is there a standard lib/package? - perennial_noob
this helped us get our jhispter app working - thanks - Gel
If you're on Windows, make sure you have the wrapper/maven-wrapper.jar - heyomi
You may check this out: github.com/takari/maven-wrapper maven.apache.org/wrapper ``` lang-powershell mvn wrapper:wrapper ``` - Néstor Waldyd
1
[+87] [2018-04-30 18:10:50] Phil Webb [ACCEPTED]

You're missing the .mvn folder in your git repository. You should have a folder called .mvn which contains the files wrapper/maven-wrapper.jar, wrapper/maven-wrapper.properties and jvm.config. Perhaps you missed it because it's a hidden folder.

Try doing git add -f .mvn from the command line then commit and push.


(1) Note that I actually had the .mvn directory already created and still had to do this for some reason. - mslissap
(1) In my case I had the .mvn directory but I needed to install git-lfs to be able to checkout properly maven-wrapper.jar (otherwise it is replaced by a text file) - Rolintocour
2
[+22] [2022-08-10 12:36:24] SbiN

«I don't know what it is used for»:

A mvnw is a maven wrapper. Basically it is a script that decouples maven from your local installation and may add checks and features to the actual mvn command - for the complete explanation check the official maven documentation [1].

To set it up (assuming you had it running solely with mvn) it should suffice to run:

    mvn wrapper:wrapper 

And that should create at least the following new files:

    .
    ├── .mvn
    │   └── wrapper
    │       ├── maven-wrapper.jar
    │       └── maven-wrapper.properties
    ├── mvnw
    └── mvnw.cmd

Then, by evoking mvnw you'd run everything by that maven-wrapper.jar - completely unattached from you local maven installation.

If the maven-wrapper.jar is not in place after the command, you probably have a network issue - the most common one is a proxy that needs to be configured.

«How to fix»:

What is the problem?

Proxy issue:

Initially, the mvn wrapper:wrapper couldn't fetch the maven-wrapper.jar for me.

.
├── .mvn
│   └── wrapper
│       └── maven-wrapper.properties
├── mvnw
└── mvnw.cmd

If you take the deep dive into the rabbit hole and check the actual wrapper code [2], you'll notice there is no default proxy - but you can set a proxy for the java virtual machine by creating a .mvn/jvm.config file containing:

    -Dhttp.proxyHost=proxy.domain.name
    -Dhttp.proxyPort=8080
    -Dhttps.proxyHost=proxy.domain.name
    -Dhttps.proxyPort=8080 

With that in place just rerunning the setup command

    mvn wrapper:wrapper

fetched everything by itself:

    .
    ├─ .mvn
    │  ├── jvm.config
    │  └── wrapper
    │      ├── maven-wrapper.jar
    │      └── maven-wrapper.properties
    ├── mvnw
    └── mvnw.cmd

By now, running the ./mvnw clean install, should work ok - it did for me (but only for a few days)

Class Issue:

One nice day, I came into office and the mvnw was returnnig the same error you stated!...

For me the issue was actually a line ending (LF <-> CRLF) issue in the jvm.config file.

As the mvnw script expands the options into an actual command [3] the command would get messed up with a CR splitting it before the classpath option. Since that classpath is where the maven-wrapper.jar (containing the MaveWrapperMain) is passed, the java command couldn't actually get the class.

How did I fix it:

I simply re-created the file containing only LFs and no CRLFs.

Bonus:

As I noticed that this CRLF was introduced by a user that was on a windows machine, I checked some git documentation [4] to avoid repeating the issue, ran the following and advised everyone to do the same:

    git config --global core.autocrlf true

Hope this centralizes the steps I found scattered and ends up saving time to someone.

[1] https://maven.apache.org/wrapper/index.html
[2] https://github.com/takari/maven-wrapper/tree/d89e40a2f5dc67fc75603a213b945de2a5ac0b14/src/main/java/org/apache/maven/wrapper
[3] https://i.sstatic.net/uO4Zj.png
[4] https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration#Formatting-and-Whitespace

Thank you for the suggestion, it got me on the path to net.properties. I was surprised that maven-wrapper seems to bootstrap before even considering proxy values in $HOME\.m2\settings.xml and I wasn't excited to place or modify a jvm.conf in each project. Maven's configure page touts jvm.conf for "options for your build on a per project base" over MAVEN_OPTS and .mavenrc file, so none of those seemed like a great fit for proxy settings. I found one reference to the JVM's net.properties. It worked and seems like the best place for proxy settings on my setup. - jla
3
[+10] [2022-04-06 15:49:11] Akilan Tamilarrasu
  1. Adding to what Oleksii Volynskyi,

use the official maven wrapper

mvn wrapper:wrapper

rather than io.takari:maven:wrapper which is no longer maintained

  1. As mentioned by Phil Webb add .mvn folder to the git repository

I was using a 5 years old project. Knowing that io.takari is no longer maintained solved my issue. (The repo is archived since 2021 github.com/takari/maven-wrapper) - Davide Pedron
4
[+5] [2021-10-30 05:00:24] HelloWorld

For anyone using Windows seeing this error, check if the .mvn folder files got converted to CRLF. If so, changing them to LF will likely fix your problem.


5
[+4] [2022-04-21 11:47:17] XSword24

In my case, I had the project saved in a folder which route included a character with an accent (in my case "á"). Removing the accent fixed the problem.


(1) That was it in my case. - user3408531
(1) Colons (:) are also problematic... - ss1
6
[0] [2023-11-02 08:24:26] Lebeau Daïkini

In my case, there was a .dockerignore file in which you select all the jar files that should be taken into account and the one in .mvn wasn't there, hence the error


7
[-2] [2020-03-17 14:21:14] Badmous

i actually did just mvn clean install and it worked, i am actually using older version of spring 2.0 to be specific


8