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

如何解决Spring Boot Hello World应用的NoClassDefFoundError错误?

Fixing 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
    The clean flag deletes old build artifacts, and -U forces a refresh of all dependencies.
  • Gradle: ./gradlew clean build --refresh-dependencies (use gradlew.bat on Windows)

3. Check Your Main Class Setup

Your main application class needs two key things to start Spring Boot correctly:

  • The @SpringBootApplication annotation (this combines @Configuration, @EnableAutoConfiguration, and @ComponentScan into one):
  • A valid main method that calls SpringApplication.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:

  1. Build the JAR with mvn package (Maven) or ./gradlew build (Gradle)
  2. 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 04:05:33