You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

移除Maven pom.xml中war解决ClassNotFoundException,求底层原理解析

Nice catch on solving this by removing the <war> packaging—let's dig into why this worked and what was causing that frustrating ClassNotFoundException in the first place.

Why Did You Get ClassNotFoundException: org.apache.commons.io.FileUtils?

This error boils down to one core issue: the JVM couldn't find the FileUtils class in its runtime classpath. For your specific scenario, there are two likely culprits tied to using war packaging:

  • Your commons-io dependency might have been marked with a provided scope. When building a war, Maven assumes provided dependencies are supplied by your web container (like Tomcat), so it doesn't package them into WEB-INF/lib. If you weren't running the app in a container (e.g., running a main method directly), there's no way the JVM could find this class.
  • War files have a structure optimized for web containers, not standalone Java apps. All dependencies live under WEB-INF/lib, but the JVM doesn't automatically scan this directory when running a regular Java program. Even if commons-io was in WEB-INF/lib, your runtime setup wouldn't pick it up.

The Underlying Logic of Removing the <war> Packaging

Maven's packaging setting dictates how it builds your project, handles dependencies, and structures the output:

  • When using <packaging>war</packaging>:
    • Maven follows web app conventions: it sorts dependencies into WEB-INF/lib (for compile/runtime scopes) or ignores them entirely (for provided scopes, since containers are supposed to supply them).
    • This structure works great for deploying to Tomcat or Jetty, but fails for standalone runs—since the JVM doesn't know to look in WEB-INF/lib for classes.
  • When you remove the war packaging (Maven defaults to jar):
    • Maven switches to handling dependencies like a standard Java application:
      • All compile and runtime dependencies are included in the runtime classpath. If you run the app via Maven's exec plugin, it automatically adds these dependencies to the JVM's classpath arguments.
      • If you build an executable JAR, plugins like maven-shade-plugin or maven-assembly-plugin will either bundle dependencies into the JAR or generate a MANIFEST.MF that points to the correct dependency paths.
    • This aligns the dependency handling with how a standalone Java program expects to find classes, so the JVM can locate FileUtils without issues.

To put it simply: Your original war packaging was optimized for web containers, but your runtime environment didn't match that context. Switching to jar packaging told Maven to use dependency rules that fit a regular Java app, fixing the classpath mismatch that caused the error.

内容的提问来源于stack exchange,提问作者yuzhen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:26:23