如何解决Spring Boot Hello World应用的NoClassDefFoundError错误?
NoClassDefFoundError for Spring Boot Hello World App Hey there, let's get your Spring Boot Hello World app up and running. That NoClassDefFoundError for org.springframework.boot.SpringApplication is almost always a sign that your project isn't pulling in the required Spring Boot dependencies properly, or something's off with your build setup. Here's how to troubleshoot step by step:
1. Verify Your Build Configuration
First, double-check your pom.xml (if using Maven) or build.gradle (if using Gradle) to make sure you have the core Spring Boot setup:
For Maven:
Ensure your pom.xml includes the Spring Boot parent dependency (this manages all dependency versions automatically):
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.2.0</version> <!-- Use the stable version from your tutorial or latest release --> <relativePath/> </parent>
Add the web starter dependency (required for web-based apps):
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
Don't forget the Spring Boot Maven plugin (critical for packaging and running the app):
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
For Gradle:
Make sure your build.gradle has the Spring Boot plugin and web starter:
plugins { id 'java' id 'org.springframework.boot' version '3.2.0' id 'io.spring.dependency-management' version '1.1.4' } dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' }
2. Refresh Dependencies & Clean Build Cache
Sometimes dependencies don't download fully, or cached files are corrupted. Run these commands to fix that:
- Maven:
mvn clean install -U
Thecleanflag deletes old build artifacts, and-Uforces a refresh of all dependencies. - Gradle:
./gradlew clean build --refresh-dependencies(usegradlew.baton Windows)
3. Check Your Main Class Setup
Your main application class needs two key things to start Spring Boot correctly:
- The
@SpringBootApplicationannotation (this combines@Configuration,@EnableAutoConfiguration, and@ComponentScaninto one): - A valid
mainmethod that callsSpringApplication.run()
Example of a correct main class:
@SpringBootApplication public class HelloWorldSpringBootApp { public static void main(String[] args) { SpringApplication.run(HelloWorldSpringBootApp.class, args); } }
Also, make sure your controller class (the one returning "Hello Nigeria.") is in the same package or a sub-package of your main class—this ensures Spring can scan and register it.
4. Run the App the Right Way
Avoid directly running the main method in your IDE unless you're sure dependencies are fully synced. Instead, use your build tool's dedicated Spring Boot run command:
- Maven:
mvn spring-boot:run - Gradle:
./gradlew bootRun
If you want to package and run as a JAR:
- Build the JAR with
mvn package(Maven) or./gradlew build(Gradle) - Run it with
java -jar target/your-app-filename.jar(replace with your actual JAR name)
5. Sync Dependencies in Your IDE
Sometimes IDEs don't pick up Maven/Gradle changes automatically:
- IntelliJ: Click the "Reload All Maven Projects" button in the right-side Maven panel, or press
Ctrl+Shift+O(Windows) /Cmd+Shift+O(Mac) to import dependencies. - Eclipse: Right-click your project →
Maven→Update Project, then check "Force Update of Snapshots/Releases" and click OK.
If you've tried all these steps and still run into issues, sharing your full pom.xml or build.gradle file will help narrow down the problem further.
内容的提问来源于stack exchange,提问作者Ikenna Shonowo

