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

Spring与Maven新手求助:运行时出现NoClassDefFoundError异常

Fixing java.lang.NoClassDefFoundError: org/springframework/core/io/Resource in Maven + Spring Project

Hey there, let's break down why you're hitting this error even though your Maven builds are passing and the Spring JARs are in your .m2 folder. This is a super common gotcha for folks new to Maven and Spring—here's what's going wrong and how to fix it:

Why this happens

When you run mvn clean compile or mvn clean install, Maven only ensures your code compiles successfully (it uses dependencies during compile time). But when you run your project directly (like with a plain java command or even Eclipse if the classpath isn't set up right), the JVM doesn't automatically know where to find all the Spring dependency JARs. The compile classpath and runtime classpath are two separate things here.

Solutions to try

This is the easiest and most reliable way because Maven will handle the runtime classpath for you automatically. You can either add the plugin to your pom.xml, or just run this command directly in your project root:

mvn exec:java -Dexec.mainClass="com.your.package.YourMainClass"

Just replace com.your.package.YourMainClass with the full qualified name of your project's main class (the one with the main() method). Maven will pull in all required Spring JARs and their transitive dependencies into the runtime classpath, so the NoClassDefFoundError should disappear.

2. Fix Eclipse's runtime classpath (if you're running from Eclipse)

You ran mvn eclipse:eclipse, but sometimes Eclipse doesn't pick up the updated classpath automatically. Try these steps:

  • Right-click your project in Eclipse → Maven → Update Project (check "Force Update of Snapshots/Releases" if needed)
  • Go to Run → Run Configurations → Select your Java Application run config
  • Switch to the Classpath tab and make sure all Spring-related JARs (spring-core, spring-context, and their dependencies like spring-beans) are listed under User Entries
  • If they're missing, re-run mvn eclipse:eclipse, refresh your project, and check again

3. Verify your dependency scopes in pom.xml

Double-check that your Spring dependencies don't have an incorrect scope set. If you accidentally used provided or test scope, those JARs won't be included in the runtime classpath. Your dependencies should look something like this (no scope tag means default compile scope, which is what you want):

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>your-spring-version-here</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>your-spring-version-here</version>
</dependency>

If you really want to use the plain java command, you'll need to manually include all Spring JARs and their transitive dependencies in the classpath. This is tedious because Spring has a lot of dependencies, but here's an example structure:

java -cp "target/classes:~/.m2/repository/org/springframework/spring-core/[your-version]/spring-core-[your-version].jar:~/.m2/repository/org/springframework/spring-context/[your-version]/spring-context-[your-version].jar:[all-other-spring-dependency-jars]" com.your.package.YourMainClass

Replace [your-version] with your actual Spring version, and make sure to add every required JAR (like spring-beans, spring-expression, etc.). Again, this is error-prone, so stick with the Exec plugin if you can.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 08:26:25