Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

Spring Boot 3 or 4 will not compile because of javax.servlet types

Your error

cannot access javax.servlet.http.HttpServletRequest3 more lines it can show

Fix it fast

Most common cause, 1 of 3

Your own classes still import javax.servlet

Rename the package in every import; class names stay the same.

Fix
// before
import javax.servlet.http.HttpServletRequest;
import javax.servlet.FilterChain;

// after
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.FilterChain;
Did this fix it?
Full steps for this cause: check, fix, verify

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.

  • Applies to Spring Boot 4.2.0, Spring Boot 3.5, Spring Framework 7.1.0, jakarta.servlet-api 6.1.0
  • Updated
  • By Maaz Zindani, Senior Software Engineer

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.

Run this first
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:

  1. 1Your own classes still import javax.servletThe compiler prints package javax.servlet.http does not exist for files in your project, and grep lists the same files
  2. 2A third-party library was compiled against javax.servletYour 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
  3. 3Spring Framework 5 jars are pinned beside a Boot 3 or 4 parentThe 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.HttpServletRequest
  • class file for javax.servlet.http.HttpServletRequest not found
  • package javax.servlet.http does not exist
  • package 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.

Check
grep -rln "javax.servlet" src/main/java src/test/java

Fix

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.

Fix
// 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.

Verify
mvn -q compile

Confirmed by github.com

Did this fix it?

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.

Check
mvn dependency:tree -Dincludes=com.example.vendor
./gradlew -q dependencyInsight --dependency vendor-lib --configuration compileClasspath

Fix

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.

Fix
<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.

Verify
mvn dependency:tree -Dincludes=javax.servlet

Confirmed by github.com

Did this fix it?

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.

Check
mvn dependency:tree -Dincludes=org.springframework
grep -n -A2 "<groupId>org.springframework" pom.xml

Fix

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.

Fix
<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.

Verify
mvn -q compile

Confirmed by raw.githubusercontent.com

Did this fix it?

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.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.