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

13

102

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.

Anchylose answered 24/3, 2013 at 17:12 Comment(2)
Note that this behavior is according to the servlet specification. A full understanding is helpful in understanding the more tricky problems you may run into.Longobard
possible duplicate of Error: Servlet Jar not Loaded... Offending class: javax/servlet/Servlet.classKarlin
R
117

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>
Rooney answered 24/3, 2013 at 17:23 Comment(7)
"The dependency should be in the provided scope, rather than the default compile scope, in your Maven pom." How can I do thatAnchylose
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.Rooney
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?Anchylose
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.Rooney
how do I do the same for ivy?Oral
look this answer https://mcmap.net/q/210405/-jar-not-loaded-see-servlet-spec-2-3-section-9-7-2-offending-class-javax-servlet-servlet-class if you are using child projects or inheritance in MavenPanthea
Documenation on Dependency ScopeSitka
M
21

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.

Megacycle answered 23/5, 2013 at 9:48 Comment(0)
P
4

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>
Permatron answered 9/4, 2015 at 3:13 Comment(0)
P
2

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
Pidgin answered 10/11, 2014 at 20:23 Comment(0)
A
2

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.

Akel answered 29/12, 2014 at 15:38 Comment(0)
M
1

I've been struggling with this issue and I've tried numerous "solutions".

However, in the end, the only one that worked and it actually took a few seconds to do it was to: delete and add back new server instance!

Basically, I right clicked on my Tomcat server in Eclipse under Servers and deleted it. Next, I've added a new Tomcat server. Cleaned and redeployed the application and I got rid of this error.

Mcadams answered 16/4, 2015 at 8:57 Comment(1)
Right click on your server -> Clean... and then again right click on your server -> clean tomcat work directory fixed it for meKnighten
G
0

Check Inside the Following Directory for the jar file el-api.jar :C:\apache-tomcat-7.0.39\lib\el-api.jar if it exists then in this directory of your web application WEB-INF\lib\el-api.jar the jar should be removed

Goran answered 24/4, 2013 at 15:55 Comment(0)
S
0

when your URL pattern is wrong, this error may be occurred.

eg. If you wrote @WebServlet("login"), this error will be shown. The correct one is @WebServlet("/login").

Seclude answered 14/6, 2014 at 8:59 Comment(0)
P
0

Exclusions and provided dependencies will not work in child projects.

If you are using inheritance in Maven projects you must include this configuration on the parent pom.xml file. You will have a <parent>...</parent> section in your pom.xml if you are using inheritance. So you will have something like this in your parent pom.xml:

<groupId>some.groupId</groupId>
<version>1.0</version>
<artifactId>someArtifactId</artifactId>
<packaging>pom</packaging>
<modules>
    <module>child-module-1</module>
    <module>child-module-2</module>
</modules>
<dependencies>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.1</version>
        <scope>provided</scope>
    </dependency>
</dependencies>
Panthea answered 19/10, 2015 at 19:50 Comment(0)
B
0

The JAX-WS dependency library “jaxws-rt.jar” is missing.

Go here http://jax-ws.java.net/. Download JAX-WS RI distribution. Unzip it and copy “jaxws-rt.jar” to Tomcat library folder “{$TOMCAT}/lib“. Restart Tomcat.

Beeline answered 20/2, 2016 at 14:5 Comment(0)
O
0

Typically when you see this message, it is benign. If it says

INFO: validateJarFile(/<webapp>/WEB-INF/lib/servlet-api-2.5.jar) - jar not loaded. 
See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class

It means it is ignoring your servlet-api-2.5.jar because tomcat already has a built-in version of that jar, so it isn't going to be using yours. Typically this doesn't cause an issue.

If however it says WEB-INF/lib/my_jar.jar - jar not loaded...Offending class: javax/servlet/Servlet.class

then what you can do (in my instance, it's a shaded jar) is run

$ mvn dependency:tree

and discover that you have a transitive dependency on "something" that depends on a jar that is either servlet-api or something like it (ex: tomcat-servlet-api-9.0.0). So add an exclusion to that to your pom, ex: (in my case, tomcat, in your case, probably the ones mentioned in the other answers):

 <dependency>
    ...
    <exclusions>
      <exclusion>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-servlet</artifactId>
      </exclusion>
    </exclusions> 
</dependency>
Outsoar answered 2/10, 2017 at 22:20 Comment(0)
V
0

from https://mcmap.net/q/212226/-how-to-prevent-duplicate-servlet-jar-using-eclipse-m2eclipse

I did mvn dependency:tree and result was

(...)
[INFO] +- org.zkoss.zk:zkspring-core:jar:3.2.0:compile
[INFO] |  +- org.springframework:spring-beans:jar:3.0.7.RELEASE:compile
[INFO] |  +- org.springframework:spring-context:jar:3.0.7.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-asm:jar:3.0.7.RELEASE:compile
[INFO] |  +- org.springframework:spring-web:jar:3.0.7.RELEASE:compile
[INFO] |  \- org.reflections:reflections:jar:0.9.5-RC2:compile
[INFO] |     +- com.google.collections:google-collections:jar:1.0:compile
[INFO] |     +- ch.qos.logback:logback-classic:jar:0.9.9:runtime
[INFO] |     |  \- ch.qos.logback:logback-core:jar:0.9.9:runtime
[INFO] |     +- com.google.code.gson:gson:jar:1.4:compile
[INFO] |     \- javax.servlet:servlet-api:jar:2.5:compile
(...)

so in pom.xml, zkspring-core I added the exclusions tag

(...)
             <groupId>org.zkoss.zk</groupId>
             <artifactId>zkspring-core</artifactId>
             <version>3.2.0</version>
+            <exclusions>
+                <exclusion>
+                <groupId>javax.servlet</groupId>
+                <artifactId>servlet-api</artifactId>
+                </exclusion>
+            </exclusions>
             </dependency>
(...)

after I ran mvn dependency:tree and javax.servlet:servlet-api:jar:2.5:compile is clean

(...)
[INFO] +- org.zkoss.zk:zkspring-core:jar:3.2.0:compile
[INFO] |  +- org.springframework:spring-beans:jar:3.0.7.RELEASE:compile
[INFO] |  +- org.springframework:spring-context:jar:3.0.7.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-asm:jar:3.0.7.RELEASE:compile
[INFO] |  +- org.springframework:spring-web:jar:3.0.7.RELEASE:compile
[INFO] |  \- org.reflections:reflections:jar:0.9.5-RC2:compile
[INFO] |     +- com.google.collections:google-collections:jar:1.0:compile
[INFO] |     +- ch.qos.logback:logback-classic:jar:0.9.9:runtime
[INFO] |     |  \- ch.qos.logback:logback-core:jar:0.9.9:runtime
[INFO] |     \- com.google.code.gson:gson:jar:1.4:compile
(...)
Vallejo answered 24/11, 2021 at 11:25 Comment(0)
P
-4

Remove servlet.jar from source web-inf/lib folder as it is available in tomcat lib folder then it works fine

Precinct answered 13/8, 2015 at 12:41 Comment(1)
This answer does not add anything that isn't already in this answer.Kalie

© 2022 - 2024 — McMap. All rights reserved.