Home » Java » The superclass “javax.servlet.http.HttpServlet” was not found on the Java Build Path [duplicate]

The superclass “javax.servlet.http.HttpServlet” was not found on the Java Build Path [duplicate]

Posted by: admin November 2, 2017 Leave a comment

Questions:

This question already has an answer here:

Answers:

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

Questions:
Answers:

Include servlet-api-3.1.jar in 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'
    }
    
Questions:
Answers:

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

Questions:
Answers:

Include servlet-api.jar from your server lib folder.

Enter image description here

Do this step

Enter image description here

What is it?

The servlet-api JAR file is a library which contains the interfaces and classes of the Servlet API specification. The servlet-api JAR file contains only the interface (the API) of the Servlet Specification, so you can use it to develop your web application.

Where can you get it?

It is provided at the link below:

http://download.oracle.com/otndocs/jcp/servlet-3.0-fr-eval-oth-JSpec/

Where it is contained/bundled

Servlet-api.jar is part of the Java EE download, so you can develop your web applications (you could not compile your FirstServlet class if the Java EE would not contain it).

Servlet containers (like Tomcat, JBoss, GlassFish, etc.) also contain the servlet-api.jar, else they would not be able to run your web application, and moreover they also contain the implementation of the interfaces that are part of the Servlet API.

The name is not always the same though, and it might not even exist as a separate JAR file, the Servlet API classes might be bundled in another JAR file.

You can however download a separate JAR file containing only the Servlet API if you just want to develop a web application for a Servlet container, or if you want to create/write your own Servlet API implementation. Look at here:

http://download.oracle.com/otndocs/jcp/servlet-3.0-fr-eval-oth-JSpec/

Portability

You can compile your web application if you have the Servlet API, no matter where it comes from. After you compiled your web application, you can optionally pack it into a WAR file (WAR=Web ARchive) which is simply a ZIP file containing your static files, your compiled Java classes and configuration files like web.xml, etc. And you will be able to run your compiled web application in any Servlet containers.

Questions:
Answers:

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

Questions:
Answers:

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)

Questions:
Answers:

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

Questions:
Answers:

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>

Questions:
Answers:

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

Questions:
Answers:

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.

Questions:
Answers:

Select project → PropertiesProject FacetsTarget RuntimesVMware Server.

It worked for me.

Questions:
Answers:

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!

Questions:
Answers:

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

Questions:
Answers:

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

Questions:
Answers:

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…