user2607498
user2607498

Reputation: 129

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

I'm trying to make a web-service with Tomcat and Eclipse using jersey library. This is my service class:

package com.gontuseries.university;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;


@Path("/university")
public class UniversityRestWs {

    @GET
    @Produces(MediaType.TEXT_HTML)
    public String getHtmlUniversityInfo(){
        return "<html><body>test</body></html>";
    }

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getTextUniversityInfo(){
        return "test";
    }
}

And this is file web.xml

 <servlet>
 <servlet-name>Jersey REST Service</servlet-name>
 <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
 <init-param>
 <param-name>com.sun.jersey.config.property.packages</param-name>
 <param-value>com.gontuseries.university</param-value>
 </init-param>
 </servlet>
 <servlet-mapping>
 <servlet-name>Jersey REST Service</servlet-name>
 <url-pattern>/rest/*</url-pattern>
 </servlet-mapping>

When i test my service i get java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer I downloaded jersey from https://jersey.java.net/download.html

Can someone help me? Thanks

Upvotes: 8

Views: 50867

Answers (10)

Swarit Agarwal
Swarit Agarwal

Reputation: 2648

We need to use provide build path to Eclipse. That must resolve issue.

Make sure it has access to all dependencies in: project -> properties -> development assembly -> add -> java build path entries --> maven dependency

jersey bundle dependency

<dependency>
   <groupId>com.sun.jersey</groupId>
   <artifactId>jersey-servlet</artifactId>
   <version>1.17</version>
<dependency>

If you are using maven, best few simple steps will work your application

Upvotes: 13

Rogger296
Rogger296

Reputation: 147

Above provided solutions are correct but even after making these changes i was getting this error. So what I did was removed all the maven downloaded Jersey jars and update Project again with new maven dependency and that resolved my issue

Upvotes: 0

ygesher
ygesher

Reputation: 1161

I was getting this error in an Eclipse project that had previously worked fine, running on Tomcat 7.0. Eventually I solved it by removing the project from Tomcat and adding it back again.

Upvotes: 0

Sachin G N
Sachin G N

Reputation: 21

We get this error because of build path issue and outdated dependency names.Latest one's start from org.glassfish and not com.sun.After replacing with latest dependencies for 2.0, 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 --> Build Path --> Add Library --> Server Runtime --> Apache Tomcat v7.0

Thanks, Sachin G N

Upvotes: -1

Indu
Indu

Reputation: 11

I got the same error -"java.lang.ClassNotFoundException : com.sun.jersey.spi.container.servlet.ServletContainer", while trying to execute restful jersey web service in tomcat from eclipse.

Following steps helped me to resolve the issue :

  • Correct the maven dependency to right version of dependent jars

         <dependency>
         <groupId>com.sun.jersey</groupId>
         <artifactId>jersey-server</artifactId>
         <version>1.19</version>
         </dependency>
         <dependency>
         <groupId>com.sun.jersey</groupId>
         <artifactId>jersey-servlet</artifactId>
         <version>1.19</version>
         </dependency>
    
  • Right click on the eclipse project -> Maven -> Update Project

  • Ensure in eclipse properties development assembly is configured correctly. Right click on project - Properties -> development assembly -> Add -> java build path entries -> maven dependency

Upvotes: 1

raj
raj

Reputation: 1

If you are using JERSEY-jaxrs-ri-2.9 version zip, Use the given below in WEB.XML file.

<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
  <param-name>jersey.config.server.provider.packages</param-name>
   ......
</init-param>

Upvotes: 0

J&#225;n Halaša
J&#225;n Halaša

Reputation: 8431

The com.sun.jersey.spi.container.servlet.ServletContainer class is contained in the "jersey-servlet.jar" so you should put it to WEB-INF/lib along with the "jersey-server.jar".

Upvotes: 4

bickster
bickster

Reputation: 1290

Are you using Jersey 2.x? If so, the ServletContainer class has been moved to another package. This article has some good info on how to change your web.xml when migrating from Jersey 1.7 to 2.x: https://java.net/projects/jersey/lists/users/archive/2013-06/message/91.

Upvotes: 5

Natan Lot&#233;rio
Natan Lot&#233;rio

Reputation: 718

Man, I had the same problem. I solve id adding the jersey-servlet-1.18.jar to the WEB-INF/lib. Then, this folder saw like this:

.
Remember, add all this jars to yout build-path

Upvotes: 7

Eran
Eran

Reputation: 360

For me the problem was that the server had no jars in the lib folder. Make sure it has access to all dependencies in: project -> properties -> development assembly -> add -> java build path entries.

Upvotes: 13

Related Questions