Home > Java > javaTutorial > body text

How to Fix \'NoClassDefFoundError\' on Maven Dependency When Running from Command Line?

Barbara Streisand
Release: 2024-10-25 09:07:02
Original
235 people have browsed it

How to Fix

How to Fix "NoClassDefFoundError on Maven Dependency"

When attempting to run a Maven project from the command line, you may encounter the NoClassDefFoundError, indicating that a required dependency is missing.

The main reason for this error is that Maven doesn't automatically include dependencies in the JAR file it builds. To resolve this, you need to "shade" the library code into your output JAR file.

To achieve this, you can use the maven-shade-plugin. Here's how to add it to your POM:

<code class="xml"><project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.5.2</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  ...
</project></code>
Copy after login

This plugin will automatically create an "uber-JAR" containing your classes and the library dependencies.

Once the plugin is registered, you can run the following commands:

<code class="sh">$ mvn package
$ java -cp target/bil138_4-0.0.1-SNAPSHOT.jar tr.edu.hacettepe.cs.b21127113.bil138_4.App</code>
Copy after login

If the error persists, you can further configure the shade plugin to specify which JARs should be included, or to set a Main-Class for an executable JAR file. Refer to the maven-shade-plugin documentation for more information.

The above is the detailed content of How to Fix \'NoClassDefFoundError\' on Maven Dependency When Running from Command Line?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!