jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class

182,472

Solution 1

The servlet API .jar file must not be embedded inside the webapp since, obviously, the container already has these classes in its classpath: it implements the interfaces contained in this jar.

The dependency should be in the provided scope, rather than the default compile scope, in your Maven pom:

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

Solution 2

You get this warning message when the servlet api jar file has already been loaded in the container and you try to load it once again from lib directory.

The Servlet specs say you are not allowed to have servlet.jar in your webapps lib directory.

  • Get rid of the warning message by simply removing servlet.jar from your lib directory.
  • If you don't find the jar in the lib directory scan for your build path and remove the jar.

C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\project\WEB-INF\lib

If you are running a maven project, change the javax.servlet-api dependency to scope provided in you pom.xml since the container already provided the servlet jar in itself.

Solution 3

To fix it, set the scope to provided. This tells Maven use code servlet-api.jar for compiling and testing only, but NOT include it in the WAR file. The deployed container will “provide” the servlet-api.jar at runtime.

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

Solution 4

You may find the following windows command line useful in tracking down the offending jar file. it creates an index of all the class files in all the jars in the folder. Execute from within the lib folder of your deployed app, then search the index.txt file for the offending class.

for /r %X in (*.jar) do (echo %X & jar -tf %X) >> index.txt

Solution 5

Maven Dependency Scope

provided : This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.

Share:
182,472

Related videos on Youtube

dreambigcoder
Author by

dreambigcoder

Updated on November 25, 2021

Comments

  • dreambigcoder
    dreambigcoder over 2 years

    I am running a Maven project which is also a dynamic web project. I have used all Spring libraries in Maven. I created web.xml, but when I start my Tomcat 7 server I am getting the following message:

    INFO: validateJarFile(C:\Users\mibvzd0\workspace\.metadata\.plugins\
    org.eclipse.wst.server.core\tmp2\wtpwebapps\hapi_hl7\WEB-INF\lib\
    servlet-api-2.4.jar) - jar not loaded.
    See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class
    

    I tried deleting the servlet from webapp/lib, but it didn't work. Let me know what should be done in my case.

  • dreambigcoder
    dreambigcoder about 11 years
    "The dependency should be in the provided scope, rather than the default compile scope, in your Maven pom." How can I do that
  • JB Nizet
    JB Nizet about 11 years
    See my edited answer. clean and build the app using Maven, and check that the servlet jar is not in the WEB-INF/lib directory in the generated webapp.
  • dreambigcoder
    dreambigcoder about 11 years
    I did everything. yet I am finding the jar in WEB-INF/lib directory. If I remove the jar I am getting below message INFO: Starting Servlet Engine: Apache Tomcat/7.0.12 Is there anything I need to do?
  • JB Nizet
    JB Nizet about 11 years
    Maybe you have a dependency that has a transitive dependency to another servlet jar version. Use mvn dependency:tree to find where this servlet jar comes from, and add an exclusion.
  • Dinesh Babu K G
    Dinesh Babu K G over 9 years
    how do I do the same for ivy?
  • Glorfindel
    Glorfindel almost 9 years
    This answer does not add anything that isn't already in this answer.
  • lmiguelmh
    lmiguelmh over 8 years
    look this answer stackoverflow.com/a/33222718/2692914 if you are using child projects or inheritance in Maven
  • Steve Pitchers
    Steve Pitchers about 8 years
    Documenation on Dependency Scope
  • kiltek
    kiltek over 6 years
    Right click on your server -> Clean... and then again right click on your server -> clean tomcat work directory fixed it for me