SpringBoot改包名后Tomcat启动报错,恢复包名仍无法解决
NoSuchMethodError: javax.servlet.ServletContext.getVirtualServerName() After Package Name Change Hey there, it’s so frustrating when a simple package name tweak breaks everything—even after you revert the change! Let’s walk through the most likely fixes for this issue, since your setup (Servlet API 3.1.0 + Tomcat 9) should fully support getVirtualServerName() without problems.
Common Fixes to Try
1. Wipe All Caches & Rebuild From Scratch
Package name changes often leave stale compiled classes or cached dependencies hanging around. Let’s clean every trace:
- For Maven projects: Run this command to clear build artifacts and force-refresh dependencies:
mvn clean install -U - For Gradle projects: Use this instead:
./gradlew clean build --refresh-dependencies - IDE Cache: Don’t forget your IDE’s cache! In IntelliJ, go to
File > Invalidate Caches / Restart; in Eclipse, useProject > Clean. This ensures your IDE isn’t clinging to old compiled class files.
2. Fix Dependency Conflicts
Even if you’re using Servlet API 3.1.0, another dependency might be pulling in an older version (like 2.5) that overrides it.
- Check your dependency tree:
- Maven:
mvn dependency:tree - Gradle:
./gradlew dependencies
Look for anyjavax.servlet:servlet-apientries with a version lower than 3.1.0.
- Maven:
- Exclude conflicting dependencies: If you find an old version being pulled in, add an exclusion to that dependency in your
pom.xmlorbuild.gradle. For example:<!-- Maven example --> <dependency> <groupId>com.some.dependency</groupId> <artifactId>problematic-artifact</artifactId> <exclusions> <exclusion> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> </exclusion> </exclusions> </dependency> - Remove manual Servlet API dependency: Spring Boot’s
spring-boot-starter-webalready includes the correct Servlet API version for Tomcat 9. If you addedservlet-api.jarmanually, try removing it—this avoids conflicts with the embedded Tomcat’s built-in dependencies.
3. Verify Tomcat’s Lib Directory
Sometimes the issue isn’t your project, but the Tomcat installation itself:
- Check your Tomcat 9
libdirectory forjavax.servlet-api.jar—confirm it’s version 3.1.0. - Make sure there are no other old
servlet-api.jarfiles in this directory (or any other Tomcat-related folders) that could be loaded instead.
4. Double-Check Spring Boot’s Package Scanning
Even after reverting the package name, it’s possible Spring Boot isn’t scanning all your classes correctly:
- Ensure your main application class (annotated with
@SpringBootApplication) is in the root package of your project. If not, explicitly set the scan path:@SpringBootApplication(scanBasePackages = "com.your.original.package") public class YourApplication { public static void main(String[] args) { SpringApplication.run(YourApplication.class, args); } }
5. Reset IDE Run Configurations
IDE run configurations can cache old classpaths or package references. Try:
- Deleting your existing run configuration for the Spring Boot app.
- Creating a new run configuration from scratch (select your main class and let the IDE auto-configure the classpath).
Why This Happens
Changing package names can trigger subtle classloader changes—even after reverting, cached classes or conflicting dependencies might still be using the old (or wrong) version of the Servlet API. The fixes above target these common culprits.
内容的提问来源于stack exchange,提问作者bdeane

