Spring Boot 3 or 4 will not compile because of javax.servlet types
Fix it fast
Most common cause, 1 of 3Your own classes still import javax.servlet
Rename the package in every import; class names stay the same.
// before
import javax.servlet.http.HttpServletRequest;
import javax.servlet.FilterChain;
// after
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.FilterChain;Short answer
cannot access javax.servlet.http.HttpServletRequest means part of your build was compiled for the old Java EE servlet API, while Spring Boot 3 and 4 ship only Jakarta Servlet (6.1 in Boot 4.2.0). Change your own javax.servlet imports to jakarta.servlet, then upgrade or replace any library or pinned Spring 5 jar that was still built against javax.
Which cause do you have?
Search your own sources for the old package, then read the exact wording of each compiler error. A package does not exist message points at your imports; a cannot access message points at code you call into.
grep -rn "import javax.servlet" src/
mvn -q compile 2>&1 | grep -E "javax.servlet|does not exist|cannot access"Then find the line you see in this list and open its fix:
- 1Your own classes still import javax.servlet
The compiler prints package javax.servlet.http does not exist for files in your project, and grep lists the same files - 2A third-party library was compiled against javax.servlet
Your sources have no javax.servlet imports, and the cannot access line sits on a call into a library class, such as a filter base class or a helper that takes the request - 3Spring Framework 5 jars are pinned beside a Boot 3 or 4 parent
The tree shows org.springframework artifacts at 5.x, while Boot 4.2.0 expects spring-webmvc 7.1.0
What does the error look like?
cannot access javax.servlet.http.HttpServletRequestclass file for javax.servlet.http.HttpServletRequest not foundpackage javax.servlet.http does not existpackage javax.servlet does not exist
Cause 1 of 3
Your own classes still import javax.servlet
You seeThe compiler prints package javax.servlet.http does not exist for files in your project, and grep lists the same files
Check
After the jump to Boot 3, a package does not exist error for javax.servlet tells you the import lines in your own code were never renamed. Boot 3.0 moved to the Jakarta EE 10 specifications, which live under jakarta.* package names, so the old ones are simply missing from the classpath now.
grep -rln "javax.servlet" src/main/java src/test/javaFix
Rename the package in every import; class names stay the same. The Boot migration guide also points to IntelliJ IDEA's migration support and OpenRewrite recipes if there are many files.
// before
import javax.servlet.http.HttpServletRequest;
import javax.servlet.FilterChain;
// after
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.FilterChain;Verify
grep returns nothing for javax.servlet under src, and the build compiles.
mvn -q compileConfirmed by github.com
Cause 2 of 3
A third-party library was compiled against javax.servlet
You seeYour sources have no javax.servlet imports, and the cannot access line sits on a call into a library class, such as a filter base class or a helper that takes the request
Check
javac reports cannot access, with class file for javax.servlet.http.HttpServletRequest not found, when a class you use was built against a type that is no longer on the classpath. The Boot guide warns that old Java EE dependencies must not remain in the build, either directly or transitively. Find the library that owns the class on the reported line and see which version you have.
mvn dependency:tree -Dincludes=com.example.vendor
./gradlew -q dependencyInsight --dependency vendor-lib --configuration compileClasspathFix
Move to the library's release built for Jakarta EE, or swap the library for one that supports Boot 3 and later. Adding javax.servlet-api to silence the compiler is a trap: Spring will still hand that code jakarta.servlet objects at runtime.
<dependency>
<groupId>com.example.vendor</groupId>
<artifactId>vendor-lib</artifactId>
<version><!-- the release its docs list for Jakarta EE / Spring Boot 3 --></version>
</dependency>Verify
The compile succeeds, and the dependency tree has jakarta.servlet-api with no javax.servlet-api anywhere.
mvn dependency:tree -Dincludes=javax.servletConfirmed by github.com
Cause 3 of 3
Spring Framework 5 jars are pinned beside a Boot 3 or 4 parent
You seeThe tree shows org.springframework artifacts at 5.x, while Boot 4.2.0 expects spring-webmvc 7.1.0
Check
Spring Framework 6.0 was the release that raised the baseline to Jakarta EE 9, so a 5.3 jar on the classpath still expects javax types, and any class you extend from it drags them into your compile. That happens when the pom or build file gives a Spring module its own version number instead of letting the Boot BOM pick it.
mvn dependency:tree -Dincludes=org.springframework
grep -n -A2 "<groupId>org.springframework" pom.xmlFix
Delete the version tags on Spring modules so Boot's dependency management supplies matching ones. Remove any old Spring version property that a previous upgrade left behind.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<!-- no <version>: the Boot parent manages it -->
</dependency>Verify
Every org.springframework artifact in the tree now carries the version that Boot manages, and the javax.servlet errors are gone.
mvn -q compileConfirmed by raw.githubusercontent.com
How do I stop it coming back?
- Add a CI step that runs mvn dependency:tree -Dincludes=javax.servlet and fails the job when it prints any artifact.
- Before upgrading Boot, list every library that touches the request or response and confirm each one has a Jakarta release.
- Never give Spring Framework or Spring Security modules their own version numbers in a Boot project.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: Dispatch Pro.