简体   繁体   中英

Maven Exception NoClassDefFoundError ClassNotFoundException but Maven Dependency Exists Command Line

I am having a problem with maven. I have included a dependency as such in my pom.xml:

<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-csv</artifactId>
  <version>1.6</version>
</dependency>

I am using Intellij as an IDE, and I get no compile warnings there or anything. I am using the command line to run the maven commands, and I can run mvn install compile package all without trouble.

However, when I try running the jar as such:

java -cp target/stride-1.0-SNAPSHOT.jar com.myapp.maven.App

I get this error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/csv/CSVFormat
    at com.stride.maven.App.parseCsv(App.java:43)
    at com.stride.maven.App.main(App.java:25)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.csv.CSVFormat
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

Clearly, maven cannot find that path. I tried deleting the .m2, rebuilding, and mostly everything I have found on stackoverflow, but I cannot find the issue, or get visibility into the issue. Note, in my Intellij I can see the dependency in the external libraries.

I have also tried using shade to copy the dependancies to the jar:

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-shade-plugin</artifactId>
      <executions>
        <execution>
          <goals>
            <goal>shade</goal>
          </goals>
          <configuration>
            <shadedArtifactAttached>true</shadedArtifactAttached>
            <transformers>
              <transformer implementation=
                                   "org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                <mainClass>com.myapp.maven.App</mainClass>
              </transformer>
            </transformers>
          </configuration>
        </execution>
      </executions>
    </plugin>

Following which I reran mvn clean install package and then my build command. No luck.

I have also tried Maven Assembly plugin. Guess what, no luck!

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
            <configuration>
                <archive>
                <manifest>
                    <mainClass>
                        com.myapp.maven.App
                    </mainClass>
                </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </execution>
    </executions>
</plugin>

I confirmed the dependencies are not even getting built in.

Thanks!

Maven works great in this case. However, you seem to miss the point of dependency.

When you define a dependency maven uses it during the compilation (hence no errors in both intelliJ and when you execute mvn install )

But this doesn't mean that dependency is placed right into the jar.

There are more complicated packaging types of application where it indeed works like this (dependant jars are included into the artifact) for example Spring boot application, Web Archives (WAR) and so forth. But since you compile a regular JAR it will include only the classes of your module and won't include classes of commons-csv in this case.

So in order to be able to run this application you should chose one of:

  • Put Jar of commons-csv (as well as other dependencies that you might have) into class path: java -classpath <path-to-commons-csv> -jar YourApp

  • Use Maven Shade Plugin to create a jar that will indeed include all the dependencies as classes

As mentioned in the other answers, you need to package up your dependencies in your executable jar (aka Uber-JAR). You mention you have tried two ways: using the Maven assembly with a descriptorRef jar-with-dependencies and the Maven shade plugin.

First global remark: Maven plugin configuration can be defined in a <pluginManagement> block or directly in the <build> part of a POM. I would suggest to put them in the <build> , and I assume you already put them there, but I can't verify that since you only pasted a part of it in your question.

When using the jar-with-dependencies descriptorRef in the Maven assembly plugin, two jar files will be created in your target/ folder: stride-1.0-SNAPSHOT.jar and stride-1.0-SNAPSHOT-with-dependencies.jar. You should use the -with-dependencies.jar , so run java -cp target/stride-1.0-SNAPSHOT-with-dependencies.jar com.myapp.maven.App

The Shade plugin offers more options than the Maven assembly plugin. However in the code you pasted you have not bound the execution of the Maven Shade plugin to the package phase of Maven's lifecycle. If you run a mvn package you will NOT see the Maven shade plugin as part of the build steps that Maven did. If you look more closely to mkyongs guide , you'll see that you need to include a <phase>package</phase> in your <execution> block.

Hi the problem is that when the jar is getting created the dependencies being downloaded is not being attached to the executable jar that is why there error is showing up we can add the below section in the build tag to get the dependencies attached to the executable jar and then you can execute the jar with java -jar command

maven-assembly-plugin package single ... wow.dxdatagenerator.App jar-with-dependencies -->

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM