1

When I'm attempting to build a simple test application in Eclipse using Jersey. When I try to run my app I get this exception:

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

I've researched answers to this question here on Stack Overflow and have verified these things:

It is true that the missing class does not exists in jersey-server any more. That class now exists in jersey-servlet (Notice "servlet", not "server"). So my maven dependencies now look like this:

<dependencies>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-core</artifactId>
        <version>1.19</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-servlet</artifactId>
        <version>1.19</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-server</artifactId>
        <version>1.19</version>
    </dependency>
</dependencies>

And my web.xml looks like this

<servlet>
    <servlet-name>jersey-helloworld-serlvet</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.javacodegeeks.enterprise.rest.jersey</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>jersey-helloworld-serlvet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

I can now look at my project in Eclipse and under Java Resources/Libraries/Maven Dependencies I can see the file jersey-servlet-1.19.jar and in that jar I can see the com.sun.jersey.spi.container.servlet package and within that package I can see the ServletContainer.class file.

So everything appears to be correct, and yet when the server (apache 7.0) starts it can't find the class which is clearly there.

Thank you for any advice you may have.

user3669653
  • 354
  • 1
  • 8
  • 15
  • Possible duplicate of [ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer](http://stackoverflow.com/questions/33327036/classnotfoundexception-com-sun-jersey-spi-container-servlet-servletcontainer) – Paul Samsotha Feb 06 '16 at 04:21
  • Solution here https://stackoverflow.com/a/70641059/5357845 – Anuj Dhiman Jan 09 '22 at 12:04

3 Answers3

1

It's an eclipse setup issue, not a Jersey issue.

From this thread ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer

Right click your eclipse project Properties -> Deployment Assembly -> Add -> Java Build Path Entries -> Maven Dependencies -> Finish.

So Eclipse wasn't using the maven dependencies when starting Apache.

Community
  • 1
  • 1
user3669653
  • 354
  • 1
  • 8
  • 15
  • For me it suddenly stopped working, so i removed maven dependency and add it again to work. – Fay007 Jul 12 '20 at 09:30
1

Jersey 1 uses com.sun.jersey, and Jersey 2 uses org.glassfish.jersey hence the exception.

Also note that also init-param starting with com.sun.jersey won't be recognized by Jersey 2.

Aarthi A
  • 106
  • 7
0

I resolved it by changing my dependency as below.

<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>

This is the same structure as maven libraries had it.

Parneet
  • 1
  • 1