Venter C.
—You encounter the following error in your Java application:
java.lang.NoClassDefFoundError: jakarta/servlet/http/HttpServletRequest
This error occurs when a class that your application needs at runtime is missing or cannot be found on the classpath. Specifically, it indicates that the jakarta.servlet.http.HttpServletRequest
class could not be found.
This issue often arises when migrating applications to newer versions of Jakarta EE from Java EE or using newer libraries that depend on jakarta.*
package names instead of the javax.*
packages.
Let’s ensure that your project is fully compatible with Jakarta EE by checking for the following common problems:
Ensure that your pom.xml
file (for Maven projects) and the corresponding build file for your project management tool contain the correct Jakarta Servlet dependency.
For Jakarta Servlet, you can include the following dependency:
<dependency> <groupId>jakarta.servlet</groupId> <artifactId>jakarta.servlet-api</artifactId> <version>5.0.0</version> <!-- Ensure that the version matches your needs --> <scope>provided</scope> </dependency>
If you use Gradle, add the following dependency:
dependencies { implementation 'jakarta.servlet:jakarta.servlet-api:5.0.0' // Adjust version as necessary }
Note: You can adjust the version based on your Jakarta EE requirements.
If your project was originally built using Java EE (javax.*
) and your application server or libraries were subsequently upgraded to use Jakarta EE (jakarta.*
), you may encounter compatibility issues. Ensure that all project dependencies, modules, and configurations align with Jakarta EE specifications.
Check whether the application server supports the version of Jakarta EE used by your application. Some servers are only compatible with Java EE. Using Jakarta dependencies without proper server support may lead to runtime issues.
Ensure that there are no outdated dependencies or libraries that still rely on javax.servlet.*
classes. Such mixed usage can lead to class resolution problems.
If you have a custom build process or use tools like Gradle, ensure that your build scripts reflect the correct dependencies and versions for Jakarta EE.
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.