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

无法找到或加载主类com.example.demo.DemoApplication,Spring项目运行求助

Troubleshooting "Could Not Find or Load Main Class com.example.demo.DemoApplication"

Hey there, let's work through this common Spring Boot issue together—you're already set up with STS, Maven, and Tomcat, so we just need to iron out a few configuration or build kinks. Here are the most likely fixes, ordered from easiest to check:

1. Fix Maven Build Issues First

  • Right-click your project in STS → Run As → Maven Clean to wipe old build artifacts. Then run Run As → Maven Install to rebuild the project from scratch.
  • Check if the target/classes/com/example/demo/ folder contains DemoApplication.class. If this file is missing, your build failed—look at the Maven Console output for errors (like missing dependencies or JDK version mismatches) and fix those first.

2. Verify Your Run Configuration

  • If you're launching via the IDE: Go to Run → Run Configurations, find your Spring Boot run config, and double-check the Main Class field is set to com.example.demo.DemoApplication. Sometimes imported projects auto-populate this incorrectly.
  • Alternatively, skip IDE configs entirely and run the project via Maven Terminal in STS:
    mvn spring-boot:run
    
    This uses Spring Boot's Maven plugin directly, which often bypasses IDE-specific bugs.

3. Match JDK Versions Across Project and IDE

  • Open your project's pom.xml and check the java.version property (e.g., <java.version>17</java.version>).
  • Right-click your project → Properties → Java Build Path → Libraries: Ensure the JRE System Library matches the version in your pom.
  • Also check Project Facets (under Properties) and set the Java version to the same value—mismatched versions can break class compilation.

4. Confirm Project Structure and Plugin Setup

  • Make sure DemoApplication.java is located exactly at src/main/java/com/example/demo/. If it's in the wrong directory (like src/main/resources or a misplaced package), STS won't recognize it as a main class.
  • Verify your pom.xml includes the Spring Boot Maven plugin—this is critical for packaging and identifying the main class:
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    

5. Clear STS Cache and Restart

  • Sometimes IDE glitches cause false errors: Go to Project → Clean, select your project, and run the clean.
  • If that doesn't work, restart STS entirely—this fixes a surprising number of weird IDE-related issues.

If none of these steps resolve the problem, share the full error log from your Maven build or STS console, and we can dig deeper!

内容的提问来源于stack exchange,提问作者Amat Samié

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 10:09:10