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>
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
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.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3