Maven打包未引入commons-io依赖,运行报NoClassDefFoundError异常
NoClassDefFoundError for Apache Commons IO in Your Maven Jar Hey there, I see exactly what's going on here. When you run mvn clean package, the default maven-jar-plugin only packages your application's own code into the Jar file—it doesn't include any of your external dependencies like commons-io. That's why you're getting that NoClassDefFoundError when you try to run the Jar; the JVM can't find the FileUtils class from commons-io because it's not inside the Jar.
Let's fix this with one of two reliable Maven plugins that will bundle your dependencies right into your final Jar.
Option 1: Use the Maven Assembly Plugin
This plugin creates a "fat Jar" that includes all your application code plus all required dependencies. Here's how to add it to your pom.xml:
First, add the plugin to your <build><plugins> section (note: since you have <pluginManagement> for version locking, you can keep that as-is and add this plugin under a new <plugins> block):
<build> <finalName>projet-al-drouin-guillory</finalName> <pluginManagement> <!-- Keep your existing plugin management configuration here --> </pluginManagement> <!-- Add this plugins section right after pluginManagement --> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>3.3.0</version> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>copyapp.client.mycp</mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
After updating your pom, run mvn clean package again. You'll now have two Jar files in your target directory:
projet-al-drouin-guillory.jar(the original thin Jar with only your code)projet-al-drouin-guillory-jar-with-dependencies.jar(the fat Jar with all dependencies included)
Run the fat Jar with this command:
java -jar target/projet-al-drouin-guillory-jar-with-dependencies.jar
Option 2: Use the Maven Shade Plugin
The Shade Plugin is another popular choice—it not only bundles dependencies but also helps handle potential dependency conflicts by relocating classes if needed. Here's the configuration:
Add this to your <build><plugins> section:
<build> <finalName>projet-al-drouin-guillory</finalName> <pluginManagement> <!-- Keep your existing plugin management configuration here --> </pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.4.1</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>copyapp.client.mycp</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build>
Run mvn clean package—this time, the main projet-al-drouin-guillory.jar in target will be the fat Jar with all dependencies included. Run it with:
java -jar target/projet-al-drouin-guillory.jar
Why Your Original Pom Didn't Work
Your current maven-jar-plugin configuration only sets the main class in the manifest, but it doesn't handle including dependencies. Maven's default behavior is to keep dependencies separate from your application Jar, so you need to explicitly bundle them using one of the plugins above.
Either of these solutions should resolve your NoClassDefFoundError for org.apache.commons.io.FileUtils.
内容的提问来源于stack exchange,提问作者Nemtecl

