Spring Boot 2.0.0项目启动遭遇ClassNotFoundException求助
First, let's confirm your provided pom.xml looks standard for a basic Spring Boot web setup:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.kkesley</groupId> <artifactId>springtest</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springtest</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <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> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
Your configuration looks solid, so let's walk through the most common fixes for ClassNotFoundException:
Refresh and re-download Maven dependencies
This error often pops up when dependencies are corrupted or never fully downloaded to your local Maven repo. Run this command in your project root folder:mvn clean install -UThe
-Uflag forces Maven to update snapshot dependencies, andcleanwipes out old compiled files that might be causing conflicts.Make sure your Java version matches the project
Your pom specifies<java.version>1.8</java.version>, so double-check:- Your IDE's project SDK is set to JDK 8 (e.g., in IntelliJ:
File → Project Structure → Project SDK) - Your system's
JAVA_HOMEenvironment variable points to a JDK 8 installation
Spring Boot 2.0.x supports JDK 8-10, but mismatched versions can break class loading.
- Your IDE's project SDK is set to JDK 8 (e.g., in IntelliJ:
Clear IDE caches and recompile
IDEs sometimes hold onto stale cache data that messes with class detection:- For IntelliJ: Go to
File → Invalidate Caches...and select "Invalidate and Restart" - For Eclipse: Go to
Project → Clean, then right-click your project →Maven → Update Project
- For IntelliJ: Go to
Check your main application class location
Ensure your class with@SpringBootApplicationis in the root package (likecom.kkesley). Spring Boot automatically scans components in this package and its subfolders. If your main class is nested deeper (e.g.,com.kkesley.springtest.controller), it won't pick up auto-configuration classes, leading to missing classes.Inspect for dependency conflicts
Even with starter dependencies, conflicting versions can sneak in. Run this command to generate a dependency tree:mvn dependency:treeLook for any dependencies with versions that don't match the Spring Boot parent's 2.0.0.RELEASE. If you find conflicts, explicitly set the correct version in your pom's
<properties>section.
If none of these steps resolve the issue, share the full stack trace of the ClassNotFoundException—that will help pinpoint exactly which class is missing!
内容的提问来源于stack exchange,提问作者kkesley

