ClassNotFoundException : com.sun.jersey.spi.container.servlet.ServletContainer

Problem

In Jersey development, hit following error message on Tomcat.


SEVERE: Servlet /RESTfulExample threw load() exception
  java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer
	
  at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1516)
  at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1361)
  //...

Here’s the Maven pom.xml


	<dependency>
		<groupId>com.sun.jersey</groupId>
		<artifactId>jersey-core</artifactId>
		<version>1.8</version>
	</dependency>

Solution

The “com.sun.jersey.spi.container.servlet.ServletContainer” is included in “jersey-server.jar“, not “jersey-core.jar“.

Actually, to develop REST service with Jersey, you just need to include “jersey-server.jar“, it will download the “jersey-core.jar” dependency automatically.


	<dependency>
		<groupId>com.sun.jersey</groupId>
		<artifactId>jersey-server</artifactId>
		<version>1.8</version>
	</dependency>

mkyong

Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities.

28 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Sachin G N
10 years ago

Hi All,

We get this error because of build path issue. You should add “Server Runtime” libraries in Build Path.

“java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer”

Please follow below steps to resolve class not found exception.

Right click on project –> Build Path –> Java Build Path –> Add Library –> Server Runtime –> Apache Tomcat v7.0

Thanks,

Sachin G N

cyberglad
10 years ago
Reply to  Sachin G N

tried that, it didn’t work

Ali Saleh
12 years ago

Hi,

I am using Jersey 1.17 and in this version, they moved the class to another Jar (jersey-servlet)

so the POM must have 3 entries now:

com.sun.jersey
jersey-core
1.17.1

com.sun.jersey
jersey-server
1.17.1

com.sun.jersey
jersey-servlet
1.17.1

and that should solve the issue for new Jersey versions.

regards,

Suraj Chandran
13 years ago

Note that the new versions of jersey-server does not contain this class, as they have abstracted out it in jersey-servlet.
So you will have to explicitly add jersey-servlet:

com.sun.jersey
jersey-servlet
1.14
jar
compile

Jyoti
2 years ago
Reply to  Suraj Chandran

It worked. Thanks

Dev
12 years ago
Reply to  Suraj Chandran

Thanks Suraj. This tip fixed my problem!

suresh
10 years ago

can any one help me
how to connect rest api to database using hibernate

suresh
10 years ago

how can we write rest api for searched data from database using hibernate

suresh
10 years ago

when i am deployed rest project in Tomcat i got this error com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes.

Ashu
12 years ago

I am using latest jars of 2.4.1 and have added all the jars to my webinf/lib. Still I face this issue.

I am using the “Jersey JAX-RS 2.0 RI bundle” for non-maven developers.
Can someone please help? Thanks.

Deco Oliveira
12 years ago

Thanks a lot

abhishek
12 years ago

hi,
gr8 tutorial i can now able to create rest services for both xml and json.
but when i tried with my services to support both type (XML + json )it is not working what extra jar and web-xml entry is needed to achive this.
any one has any idea.

Naveen Babu
12 years ago

Hi,

I am using JDK 1.5. I am not able to find the jersey servlet jar which is compatible for JDK 1.5.

Could you please help me on this.

Reza
12 years ago

I’ve tried this tutorial using eclipse juno + maven 3.0 + tomcat7, but I’m still get the error even the jars (jersey-server, jersey-servlet, jersey-bundle) are already there.

Reza
12 years ago
Reply to  Reza

sorry, it seems I’ve made a typo on web.xml. And now everything works fine 🙂
thank for your tutorial.

Geoff
13 years ago

@Suraj Chandran

Your solution worked for me. Thanks a ton!

mazhar
13 years ago

I tried to run the Hello world program but getting a
java.lang.ClassNotFoundException: org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap

please help

Naga
13 years ago

Hey could you please post jersy example with embeded jetty i can find examples any where

Kausal Malladi
13 years ago

Hi,
I am trying to implement a Hello World application using Jersey but no luck.
I am getting the following error. I am not using maven. Can you help me with a workaround?
SEVERE: Servlet /NameSearch threw load() exception
java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer
For details of how I implemented the app, please visit the link:
http://stackoverflow.com/questions/13240635/http-status-404-when-testing-restful-webservice-using-jersey

Jose Ayerdis
13 years ago

The “com.sun.jersey.spi.container.servlet.ServletContainer” is included in “jersey-server.jar“, not “jersey-core.jar“.

No this class is included in jersey-core but when you added to pom.xml it download the dependency jersey-core.

sapna
13 years ago

thanks it helped me a lot 🙂

Juliet
13 years ago

Thanks!!

Triguna
13 years ago

Thanks that worked for me.

Triguna
13 years ago
Reply to  Triguna

Hey It didnt work for me as I thought it worked. I am still getting the same error. Can you help me? I am using your RESTfulExample code.

Priyanka
13 years ago

I added jersey-bundle1.12.jar and it started working.. you can try the same..

Rajesh
10 years ago
Reply to  Priyanka

i can also added to jersey bundle latest jar and jersey server after that dos’nt working ….plz tell me how it work

adekcz
13 years ago
Reply to  Priyanka

Yes, they moved some class, up to date solution is to add jersey-bundle1.12.jar or as name suggest jersey-servlet1.12.jar, both should work.