I want to develop with Servlets in Eclipse, but it says that the package javax.servlet
/ jakarta.servlet
cannot be resolved. How can I add javax.servlet
/ jakarta.servlet
package to my Eclipse project?
15 Answers
Ensure you've the right Eclipse and Server version
Ensure that you're using at least Eclipse IDE for Enterprise Java developers (with the Enterprise). It contains development tools to create dynamic web projects and easily integrate servletcontainers (those tools are part of Web Tools Platform, WTP). In case you already had Eclipse IDE for Java (without Enterprise), and manually installed some related plugins, then chances are that it wasn't done properly. You'd best trash it and grab the real Eclipse IDE for Enterprise Java one.
You also need to ensure that you already have a servletcontainer installed on your machine which implements at least the same Servlet API version as the servletcontainer in the production environment, for example Apache Tomcat, Oracle GlassFish, JBoss AS/WildFly, etc. Usually, just downloading the ZIP file and extracting it is sufficient. In case of Tomcat, do not download the EXE format, that's only for Windows based production environments. See also a.o. Several ports (8005, 8080, 8009) required by Tomcat Server at localhost are already in use.
A servletcontainer is a concrete implementation of the Servlet API. Note that the Java EE SDK download at Oracle.com basically contains GlassFish. So if you happen to already have downloaded Java EE SDK, then you basically already have GlassFish. Also note that for example GlassFish and JBoss AS/WildFly are more than just a servletcontainer, they also supports JSF, EJB, JPA and all other Java EE fanciness. See also a.o. What exactly is Java EE?
Ensure that you're using the right Servlet package
The javax.*
package has been renamed to jakarta.*
package since Servlet API version 5.0 which is part of Jakarta EE 9 (Tomcat 10, TomEE 9, WildFly 22 Preview, GlassFish 6, Payara 6, Liberty 22, etc). So if you're targeting these server versions or newer, then you need to replace
import javax.servlet.*;
import javax.servlet.http.*;
by
import jakarta.servlet.*;
import jakarta.servlet.http.*;
in order to get it to compile, else you might risk to face this build error
The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
Integrate Server in Eclipse and associate it with Project
Once having installed both Eclipse for Enterprise Java and a servletcontainer on your machine, do the following steps in Eclipse:
Integrate servletcontainer in Eclipse
a. Via Servers view
Open the Servers view in the bottom box.
Rightclick there and choose New > Server.
Pick the appropriate servletcontainer make and version and walk through the wizard.
b. Or, via Eclipse preferences
Associate server with project
a. In new project
Open the Project Navigator/Explorer on the left hand side.
Rightclick there and choose New > Project and then in menu Web > Dynamic Web Project.
In the wizard, set the Target Runtime to the integrated server.
b. Or, in existing project
Rightclick project and choose Properties.
In Targeted Runtimes section, select the integrated server.
Either way, Eclipse will then automatically take the servletcontainer's libraries in the build path. This way you'll be able to import and use the Servlet API.
Never carry around loose server-specific JAR files
You should in any case not have the need to fiddle around in the Build Path property of the project. You should above all never manually copy/download/move/include the individual servletcontainer-specific libraries like servlet-api.jar
, jsp-api.jar
, el-api.jar
, j2ee.jar
, javaee.jar
, etc. It would only lead to future portability, compatibility, classpath and maintainability troubles, because your webapp would not work when it's deployed to a servletcontainer of a different make/version than where those libraries are originally obtained from.
In case you're using Maven, you need to make absolutely sure that servletcontainer-specific libraries which are already provided by the target runtime are marked as <scope>provided</scope>
. You can find examples of proper pom.xml
dependency declarations for Tomcat 10+, Tomcat 9-, JEE 9+ and JEE 8- in this answer: Tomcat 9 casting servlets to javax.servlet.Servlet instead of jakarta.servlet.http.HttpServlet
Here are some typical exceptions which you can get when you litter the /WEB-INF/lib
or even /JRE/lib
, /JRE/lib/ext
, etc with servletcontainer-specific libraries in a careless attempt to fix the compilation errors:
- java.lang.NullPointerException at org.apache.jsp.index_jsp._jspInit
- java.lang.NoClassDefFoundError: javax/el/ELResolver
- java.lang.NoSuchFieldError: IS_DIR
- java.lang.NoSuchMethodError: javax.servlet.jsp.PageContext.getELContext()Ljavax/el/ELContext;
- java.lang.AbstractMethodError: javax.servlet.jsp.JspFactory.getJspApplicationContext(Ljavax/servlet/ServletContext;)Ljavax/servlet/jsp/JspApplicationContext;
- org.apache.jasper.JasperException: The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory
- java.lang.VerifyError: (class: org/apache/jasper/runtime/JspApplicationContextImpl, method: createELResolver signature: ()Ljavax/el/ELResolver;) Incompatible argument to function
- jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class
you can simply copy the servlet-api.jar
and copy that jar files into lib folder, which is in WEB-INF.
then just clean and built your project, your errors will be solved.
**OR**
you can directly add jar files to library by using following steps.
- Right click on project.
- Go To Properties.
- Go to Java Build Path.
- Select Add Library option from tabs.
- Add Jar Files
- give path of your
servlet-api.jar
file. - Clean and build your project.
I know this is an old post. However, I observed another instance where in the project already has Tomcat added but we still get this error. Did this to resolve that:
Alt + Enter
Project Facets
On the right, next to details, is another tab "Runtimes".
The installed tomcat server will be listed there. Select it.
Save the configuration and DONE!
Hope this helps someone.
For maven projects add following dependancy :
<!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
For gradle projects:
dependencies {
providedCompile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.0.1'
}
or download javax.servlet.jar
and add to your project.
From wikipedia.
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloWorld extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">\n" +
"<html>\n" +
"<head><title>Hello WWW</title></head>\n" +
"<body>\n" +
"<h1>Hello WWW</h1>\n" +
"</body></html>");
}
}
This, of course, works only if you have added the servlet-api.jar
to Eclipse build path. Typically your application server (e.g Tomcat) will have the right jar file.
I was getting a null pointer exception during project creation related to "Dynamic Web Module".
To get the project to compile (that is, to javax.servlet
to import successfully) I had to go to project's Properties, pick Project Facets in the sidebar, tick Dynamic Web Module and click Apply.
Surprisingly, this time "Dynamic Web Module" facet installed correctly, and import started to work.
Many of us develop in Eclipse via a Maven project. If so,
you can include Tomcat dependencies in Maven via the tomcat-servlet-api
and tomcat-jsp-api
jars. One exists for each version of Tomcat. Usually adding these with scope provided
to your POM is sufficient. This will keep your build more portable.
If you upgrade Tomcat in the future, you simply update the version of these jars as well.
This could be also the reason. i have come up with following pom.xml
.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
The unresolved issue was due to exclusion of spring-boot-starter-tomcat
. Just remove <exclusions>...</exclusions>
dependency it will ressolve issue, but make sure doing this will also exclude the embedded tomcat server.
If you need embedded tomcat server too you can add same dependency with compile scope
.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>compile</scope>
</dependency>
You should above all never manually copy/download/move/include the individual servletcontainer-specific libraries like servlet-api.jar
@BalusC,
I would prefer to use the exact classes that my application is going to use rather than one provided by Eclipse (when I am feeling like a paranoid developer).
Another solution would be to use Eclipse "Configure Build Path" > Libraries > Add External Jars, and add servlet api of whatever Container one chooses to use.
And follow @kaustav datta's solution when using ant to build - have a property like tomcat.home or weblogic.home. However it introduces another constraint that the developer must install Weblogic on his/her local machine if weblogic is being used ! Any other cleaner solution?