无法找到或加载主类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 Cleanto wipe old build artifacts. Then run Run As →Maven Installto rebuild the project from scratch. - Check if the
target/classes/com/example/demo/folder containsDemoApplication.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:
This uses Spring Boot's Maven plugin directly, which often bypasses IDE-specific bugs.mvn spring-boot:run
3. Match JDK Versions Across Project and IDE
- Open your project's
pom.xmland check thejava.versionproperty (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.javais located exactly atsrc/main/java/com/example/demo/. If it's in the wrong directory (likesrc/main/resourcesor a misplaced package), STS won't recognize it as a main class. - Verify your
pom.xmlincludes 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é
相关产品推荐
相关产品推荐

