Home > Java > javaTutorial > body text

How can I resolve the \'NoClassDefFoundError\' in Maven when running from the command line?

Susan Sarandon
Release: 2024-10-27 04:01:02
Original
932 people have browsed it

How can I resolve the

Dependency Resolution via Shade Plugin for NoClassDefFoundError

In a first experience with Maven, inconsistencies between Eclipse and command-line usage can arise due to dependency management. Despite successful Maven project creation and dependency addition within Eclipse, executing the project via the command line may result in a NoClassDefFoundError.

By default, Maven packages dependencies separately and doesn't include them in the compiled JAR file. This omission causes the Java VM to fail when attempting to locate library classes during execution.

Instead of manually adding libraries to the classpath, the maven-shade-plugin provides a more efficient solution. It creates an "uber-JAR" that combines application classes with required library code.

To implement this, add the following plugin configuration to your POM:

<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>
Copy after login

After adding this configuration, rerun the following commands:

$ mvn package
$ java -cp target/bil138_4-0.0.1-SNAPSHOT.jar tr.edu.hacettepe.cs.b21127113.bil138_4.App
Copy after login

For advanced configuration options, such as specifying included JARs or an executable JAR file, refer to the "Examples" section on the maven-shade-plugin website.

The above is the detailed content of How can I resolve the \'NoClassDefFoundError\' in Maven when running from the 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