The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path [duplicate]
Asked Answered
E

14

460

I have a project created by Maven integration in Eclipse. All work fine, but in the work space in all JSP files have this:

The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path

To the first string where place:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

When I create a basic dynamic web project and JSP in - all is fine, no errors.

Excess answered 31/3, 2014 at 7:48 Comment(7)
You need to add the library in the build path. mkyong.com/maven/…Cumulous
you might have missed the javax.servlet-api dependance in pom.xmlSubmarine
https://mcmap.net/q/81413/-error-package-javax-servlet-does-not-existSubmarine
Be sure that you add it as provided dependency i.e. that it is not shipped with your web project.Patrizius
better answer at https://mcmap.net/q/17353/-how-do-i-import-the-javax-servlet-jakarta-servlet-api-in-my-eclipse-project/1851302Towards
I'd like to know why things are shipped that don't work unless you google and read the answer on stack overflow! I know, job security for the volunteers, but wow I just installed STS, created a new maven project, and the error just showed up. Not just complaining here (well maybe a bit), but why do things end up like this a large portion of the time when concerning eclipse-built solutions? (not blaming eclipse specifically, just a correlation).Tankage
I had a similar issue, which was based on the way I opened the project. Needed to import as maven project instead... details can be found here: #45357609Florey
S
695

Add a runtime first and select project properties. Then check the server name from the 'Runtimes' tab as shown in the image.

Select runtime from Project Facets as shown the in image

Superaltar answered 12/8, 2014 at 13:51 Comment(10)
Can you elaborate why this is working? How is the targeted runtime and a missing http-servlet connected? I'd be very interested in what's going on behind the curtains.Swum
This error indicates that http-servlet is not available in the project class path, once we add target-runtime to the project , http-servlet will be available in the project class-path.Superaltar
I did not have the Tomcat option in Runtimes in my Eclipse Mars, solved this in Window > Preferences > Server > Runtime Environments > Add > Apache Tomcat > chose de installation directory > InstallTizes
this is so out dated. in most case you use maven tomcat6 or 7 plugin.Depilatory
In a project without using Maven, you can clear this error by right click your project name in Eclipse Project Explorer -> Properties -> Java Build Path, and check Apache Tomcat server to add it to your Java Build Path.Account
If error still exists, select the project and press ALT + F5 to update the project.Launch
If someone is wondering where to find this window like I was.. It would appear when in 'Package Explorer' view > Right Click on your 'Project Folder' > select 'Properties'. On the Left panel, click on 'Project Facets'. There it is!Epirus
It worked for me but I also had to do a project clean + rebuild (Project -> Clean) to get rid of the problem.Musick
I have tried this solution and my problem still exists. I configured a runtime for was 8.5.Stearin
did someone found a solution?, i checked Tomcat in runtimes but the problem still there.Aeronaut
J
264

Include servlet-api-3.1.jar into your dependencies.

  • Maven

      <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
          <version>3.1.0</version>
          <scope>provided</scope>
      </dependency>
    
  • Gradle

      configurations {
          provided
      }
      sourceSets {
          main { compileClasspath += configurations.provided }
      }
      dependencies {
          provided 'javax.servlet:javax.servlet-api:3.1.0'
      }
    
Jerol answered 31/3, 2014 at 7:48 Comment(12)
a better answer would be Include servlet-api-x.y.jar in your dependencies. I though 3.0 was the latest.Craniotomy
error was gone, but on another pc i don't need do this.Excess
How is this other PC different?Holt
Only by Eclipse version, Kepler - error, Juno - ok.Excess
If you are using Maven to manage your dependencies and if the web container you deploy to (Tomcat?) doesn't differ, it should work. Do you deploy from inside Eclipse?Holt
The Servlet API Version to be used is determined by the container. So you need to know which is the traget system. To avoid classpath problems don't ship the servlet api with our artifact thus mark the depency als 'provided' in your pom.Patrizius
If you are using Maven, then it will solve the issue.Cobbie
I wonder why people downvote my answer. A comment would be useful.Holt
where can I find this file?Assorted
This answer is almost completely right. The issue: <version>3.1</version> does not exist: mvnrepository.com/artifact/javax.servlet/javax.servlet-api shows what version do exist. I suggest you edit your answer to 3.0.1or 3.1.0 (I would edit it but edits must be 6+ characters in length)Moya
3.1.0 it shall be then.Holt
Once you specify a target runtime such as tomcat, you don't need to explicitly include the javax.servlet-api.jar. It will find it in the tomcat installation folder.Leninist
D
119

Project → PropertiesTarget RuntimesApache Tomcat worked for me. There is no Target Runtimes under Facets (I'm on Eclipse v4.2 (Juno)).

Demos answered 31/7, 2014 at 16:31 Comment(0)
W
66

For an Ant project:

Make sure, you have servlet-api.jar in the lib folder.

For a Maven project:

Make sure, you have the dependency added in POM.xml.

<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>javax.servlet-api</artifactId>
  <version>3.1.0</version>
  <scope>provided</scope>
</dependency>

Another way to do it is: Update the project facets to pick up the right server.

Check this box in this location:

Project → PropertiesTarget Runtimes → Apache Tomcat (any server)

Windtight answered 10/11, 2015 at 14:35 Comment(1)
The second suggestion worked for me. The project was created but the Tomcat server was not checked in "Target Runtimes".Kern
J
29

Project → PropertiesTarget Runtimes → *Apache Tomcat worked for me. There is no Target Runtimes under Facets (I'm on Eclipse v4.4 (Luna)).

Jude answered 30/9, 2014 at 13:53 Comment(0)
P
26

Adding the Tomcat server in the server runtime will do the job:

Project PropertiesTarget Runtimes → Select your Server from the list, "JBoss Runtime" → Finish

In case of Apache you can select Apache Runtime.

Enter image description here

Polyunsaturated answered 11/5, 2015 at 7:12 Comment(0)
H
22

These steps can really help you:

  1. If you didn't install any server you have to do these steps:

    Menu WindowPreferencesExpend ServerRuntime environmentAdd → choose a name and then choose the Apache server path that you already installed on your PC (you can press download and install too) → FinishOK

Ref# for more information, click here

  1. Add the Tomcat server:

    Project PropertiesJava Build PathAdd Library → Select "Server Runtime" from the list* → Next → Select "Apache Tomcat" → Finish

Ref# This answer

Horseback answered 7/8, 2015 at 11:7 Comment(0)
L
16

Just add these dependencies to your pom.xml file:

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
</dependency>

<dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>javax.servlet.jsp-api</artifactId>
    <version>2.2.1</version>
    <scope>provided</scope>
</dependency>
Lusterware answered 23/2, 2015 at 20:43 Comment(2)
Why do we need the second dependency ? Please remove the second! (jsp-api).Xenogamy
no first one is enough.Surd
S
14

Adding the Tomcat server in the server runtime will do the job:

Project properties → Java Build PathAdd LibrarySelect "Server Runtime" from the list → Next → Select "Apache Tomcat" → Finish.

Spindle answered 26/2, 2015 at 5:39 Comment(0)
C
6

Select project → PropertiesProject FacetsTarget RuntimesVMware Server.

It worked for me.

Columbian answered 8/6, 2014 at 2:48 Comment(0)
A
5

As this is unanswered, I am guessing something other than Maven dependencies are wrong with the ops build.

While not using Maven, I have the same problem from time to time when re-creating my development environment from svn, and I always forget why and have to figure it out. Unfortunately it seems this is a problem with Eclipse.

I am able to remove all such errors from once working projects by picking just one of the dynamic web projects, or just tomcat dependent projects, and move a dependency in the build order. This seems to force all projects to rebuild properly and all of the errors are then resolved.

Right click on a web project, select "build Path" -> "Configure Build Path". Go to the tab "Order and Export", then pick a library or jar entry and move it up or down. I used the JRE System Library and moved it to the top.

Click OK, and all that red goes away!

Alexandra answered 31/7, 2014 at 17:11 Comment(0)
L
2

If you are not using Maven, just drop the javax.servlet-api.jar in your project lib folder.

Levenson answered 3/8, 2014 at 19:26 Comment(0)
G
1

In case of JBoss... right click on project → Build Java path → add external JAR files.

Then browse to jboss-folder → Commonlib → servlet-api.jar

. . Click OK, refresh the project, and run it...

Grovel answered 14/3, 2015 at 20:1 Comment(1)
Why is it only on Jboss ? Please remove that!Xenogamy
S
0

And if nothing works by whatever reason, build it from the command line:

ant -Dj2ee.server.home=D:\apache-tomcat-8.0.23 clean

ant -Dj2ee.server.home=D:\apache-tomcat-8.0.23 compile

ant -Dj2ee.server.home=D:\apache-tomcat-8.0.23 dist
Stingo answered 2/6, 2015 at 9:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.