java.lang.ClassCastException: [ServletName] cannot be cast to javax.servlet.Servlet: How to resolve?


When I was using tomcat-maven-plugin to test an app I worked on in embedded tomcat , it was erroring out with the following message:

INFO: Marking servlet TestWebServiceServlet as unavailable
Jun 17, 2011 4:01:48 PM org.apache.catalina.core.StandardContext loadOnStartup
SEVERE: Servlet /cxf threw load() exception
java.lang.ClassCastException: test.ws.TestWebServiceServlet cannot be cast to javax.servlet.Servlet
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1104)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:981)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4058)
. . .

I used Apache CXF to expose web services in this application. Here’s what I did to resolve it.

In my Maven POM xml, I had servlet-api.jar

<dependency>
<groupId>org.mortbay.jetty</groupId>
 <artifactId>servlet-api</artifactId>
 <version>2.5-6.0.1</version>
 </dependency>

It seemed to me the Servlet API jar was causing this issue. So, I just did this to solve the error – added a scope element with a value provided so that the final build doesn’t include this api jar:

<dependency>
 <groupId>org.mortbay.jetty</groupId>
 <artifactId>servlet-api</artifactId>
 <version>2.5-6.0.1</version>
 <scope>provided</scope>
 </dependency>

When it worked, it reminded me of another issue that I’ve solved and is already here in this blog:  http://singztechmusings.wordpress.com/2011/04/14/resolving-java-lang-classformaterror-absent-code-attribute-in-method-that-is-not-native-or-abstract-error/

About these ads

One comment

  1. [...] java.lang.ClassCastException: cannot be cast to javax.servlet.Servlet: How to resolve? (singztechmusings.wordpress.com) [...]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 27 other followers

%d bloggers like this: