61

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

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, 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?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
hsluoyz
  • 2,739
  • 5
  • 35
  • 59
  • Maybe take a look at [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve)... I believe nobody is willing to read and understand your code from zero – 0x1C1B Apr 30 '18 at 16:08
  • This isn't the error, you need to look at the maven output log for "mvn install". – Nyamiou The Galeanthrope Apr 30 '18 at 16:58
  • Possible duplicate of [What does "Could not find or load main class" mean?](https://stackoverflow.com/questions/18093928/what-does-could-not-find-or-load-main-class-mean) – hsluoyz May 01 '18 at 07:17

7 Answers7

119

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.

Oleksii Volynskyi
  • 1,317
  • 1
  • 8
  • 5
  • 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 Jul 19 '19 at 18:32
  • this helped us get our jhispter app working - thanks – Gel Feb 19 '20 at 16:10
  • If you're on Windows, make sure you have the `wrapper/maven-wrapper.jar` – heyomi Nov 18 '21 at 13:36
  • You may check this out: https://github.com/takari/maven-wrapper https://maven.apache.org/wrapper/ ``` lang-powershell mvn wrapper:wrapper ``` – Néstor Waldyd Feb 19 '22 at 01:39
85

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.

Phil Webb
  • 8,119
  • 1
  • 37
  • 37
  • 1
    Note that I actually had the .mvn directory already created and still had to do this for some reason. – mslissap May 04 '21 at 19:55
  • 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 Sep 22 '21 at 14:59
8

«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.

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, 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 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 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.

SbiN
  • 83
  • 1
  • 7
6
  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
5

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.

HelloWorld
  • 290
  • 3
  • 14
4

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.

XSword24
  • 41
  • 2
-2

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

Badmous
  • 95
  • 1
  • 5