java: cannot access javax.servlet.ServletException class file for javax.servlet.ServletException not found
Asked Answered
L

2

7

Description

When I develop my java project I use:
Java version: 1.8,
Apache Tomcat version 9.0.x

Then, I use Tomcat 10.0.x to run my project (with few modifications in my project). I had to change javax package related imports to jakarta package imports.

When I build the project I got this following error;

java: cannot access javax.servlet.ServletException  
class file for javax.servlet.ServletException not found

Fix

When I search for a fix, I found that I need to include Java Servlet API dependency in my pom.xml file. This way I could build the project successfully.

<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.1</version>
    <scope>provided</scope>
</dependency>

Question

  1. Why do I have to include Java Servlet API dependency when I migrate to Tomcat 10 ?
  2. I did include Java Servlet API dependency, but is that a proper fix?
Lickspittle answered 20/1, 2022 at 7:44 Comment(2)
This has been answered by #64387972 It will explain why you are having issues.Fulmer
What you need is jakarta.servlet-api version 5 or higher.Homoousian
U
5

Why do I have to include Java Servlet API dependency when I migrate to Tomcat 10 ?

Presumably because your application has a dependency on something that conforms to the JSP 4.0.x specs.

I did include Java Servlet API dependency, but is that a proper fix?

No it is not the proper fix. While you have succeeded in building your app, you will most get classloader errors when you deploy to Tomcat 10. That's because the Tomcat 10 runtime libraries don't provide the javax.servlet.* classes.

The correct approach is to identify why your application or its dependencies are trying to use classes in javax.servlet ... and eliminate this.

  1. Remove the javax.servlet / javax.servlet-api / 4.0.1 dependency.

  2. Search your application codebase for any remaining references to javax.servlet ... and change them.

  3. Use the Maven dependency tree plugin to try to identify which of your applications dependencies has a dependency on the old javax.servlet API. Look for newer versions of those dependencies that use jakarta.servlet instead.

If 2 and 3 don't identify the culprit, it gets rather difficult. You might need to unpack all of the dependent JAR files and "analyze" them using javap and grep. Or there might be better way.

If you are able to identify the dependencies, but you can't find a Jakarta-compatible version, then you have another kind of problem. You have to choose between putting in the effort to port the dependency, finding another library (or whatever) to provide the functionality, or remove the functionality from your web app.

Ungava answered 20/1, 2022 at 11:44 Comment(0)
S
0

Updating all the dependencies in pom.xml as per the suggestions in IDE worked for me.

Svetlana answered 21/1 at 8:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.