You need to add the path to Tomcat’s /lib/servlet-api.jar
file to the compile time classpath.
javac -cp .;/path/to/Tomcat/lib/servlet-api.jar com/example/MyServletClass.java
The classpath is where Java needs to look for imported dependencies. It will otherwise default to the current folder which is included as .
in the above example. The ;
is the path separator for Windows; if you’re using an Unix based OS, then you need to use :
instead.
If you’re still facing the same complation error, and you’re actually using Tomcat 10 or newer, then you should be migrating the imports in your source code from javax.*
to jakarta.*
.
import jakarta.servlet.*;
import jakarta.servlet.http.*;
In case you want to keep using javax.*
for whatever reason, then you should be downgrading to Tomcat 9 or older as that was the latest version still using the old javax.*
namespace.
See also:
- jakarta.servlet.ServletException: Class [com.practice.MyServlet] is not a Servlet
- How do I import the javax.servlet / jakarta.servlet API in my Eclipse project?
- What exactly is Java EE?