Spring Boot报错java.lang.NoClassDefFoundError: org/springframework/util/Assert求助
java.lang.NoClassDefFoundError: org/springframework/util/Assert in Spring Boot Hey there! Let's work through this error you're hitting—it's a common hiccup when starting out with Spring Boot and Maven, so you're not alone. That NoClassDefFoundError usually means Maven didn't properly download the Spring Core dependencies your app needs to run. Here's how to fix it step by step:
Step 1: Force Maven to Refresh and Re-download Dependencies
First, let's rule out cached or missing dependencies. Open your terminal (or use your IDE's built-in terminal) and run this command in your project root folder:
mvn clean install -U
The -U flag forces Maven to update all snapshot and release dependencies, replacing any corrupted or missing files in your local Maven repository.
Step 2: Verify Your Parent Dependency Configuration
Double-check that your spring-boot-starter-parent is set up correctly in your pom.xml. Stick to a stable, non-snapshot version (avoid bleeding-edge builds when you're just starting out):
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.14</version> <!-- Use a stable release version --> <relativePath/> <!-- Lets Maven pull directly from the central repository --> </parent>
This parent dependency manages all the versions of Spring Boot's starter libraries. If it's misconfigured, your spring-boot-starter-web won't pull in the correct spring-core JAR that contains the Assert class.
Step 3: Sync Your IDE with Maven Dependencies
IDEs like IntelliJ IDEA or Eclipse sometimes don't automatically pick up Maven changes. Here's how to force a sync:
- IntelliJ: Click the "Reload All Maven Projects" button (the circular arrow icon) in the right-side Maven panel.
- Eclipse: Right-click your project → Maven → Update Project, then check the box for "Force Update of Snapshots/Releases" and click OK.
Step 4: Check Dependency Scopes
Make sure you haven't modified the default scope for your starter dependencies. Your pom.xml dependencies should look like this (no need to add extra scopes unless you have a specific reason):
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <!-- This is the default, keeping it explicit is fine --> </dependency> </dependencies>
If you changed the scope of spring-boot-starter-web to something like provided, it won't be included in the runtime classpath, leading to that missing class error.
Step 5: Clean and Rebuild Your Project
After the above steps, run these commands to ensure a fresh build:
mvn clean mvn package
Then try running your Application.java main class again.
The Assert class is part of Spring Core, which is a transitive dependency of spring-boot-starter-web—so once your dependencies are properly downloaded and synced, this error should disappear.
内容的提问来源于stack exchange,提问作者user182944

