score:158

Accepted answer

you have downloaded jersey 2 (which ri of jax-rs 2). the tutorial you're referring to uses jersey 1. download jersey 1.17.1 from (here), should be sufficient for you.

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.

edit

registering resources and providers in jersey 2 contains additional info on how to register classes/instances in jersey 2.

score:0

i encountered the same error today although i was using jersey 1.x, and had the right jars in my classpath. for those who'd like to follow the vogella tutorial to the letter, and use the 1.x jars, you'd need to add the jersey libraries to web-inf/lib folder. this will certainly resolve the problem.

score:0

you need to add jersey-bundle-1.17.1.jar to lib of project

<servlet> <servlet-name>jersey rest service</servlet-name> <servlet-class>com.sun.jersey.spi.container.servlet.servletcontainer</servlet-class> <!-- <servlet-class>org.glassfish.jersey.servlet.servletcontainer</servlet-class> --> <init-param> <param-name>com.sun.jersey.config.property.packages</param-name> <!-- <param-name>jersey.config.server.provider.packages</param-name> --> <param-value>package.package.test</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>

score:0

you must replace in your 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.test.myproject</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

for this:

<servlet>
    <servlet-name>jersey rest service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.servletcontainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.test.myproject</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

this is jersey 2.x uses org.glassfish.jersey packages instead of com.sun.jersey (which is used by jersey 1.x) and hence the exception. note that also init-param starting with com.sun.jersey won't be recognized by jersey 2.x once you migrate to jax-rs 2.0 and jersey 2.x

if at any moment you use maven, your pom.xml would be this:

<dependency>
    <groupid>org.glassfish.jersey.core</groupid>
    <artifactid>jersey-server</artifactid>
    <version>2.x</version>
</dependency>

replace 2.x for your desire version, e.g. 2.15

score:0

a simple workaround is , check whether you have dependencies or libs in deployment assembly of eclipse.probably if you are using tomcat , the server might not have identified the libs we are using . in that case specify it explicitly in deployment assembly.

score:0

coming back to the original problem - java.lang.classnotfoundexception: com.sun.jersey.spi.container.servlet.servletcontainer

as rightly said above, in jax 2.x version, the servletcontainer class has been moved to the package - org.glassfish.jersey.servlet.servletcontainer. the related jar is jersey-container-servlet-core.jar which comes bundled within the jaxrs-ri-2.2.1.zip

jax rs can be worked out without mvn by manually copying all jars contained within zip file jaxrs-ri-2.2.1.zip (i have used this version, would work with any 2.x version) to web-inf/lib folder. copying libs to right folder makes them available at runtime.

this is required if you are using eclipse to build and deploy your project.

score:0

in pom.xml file we need to add

<dependency>
    <groupid>com.sun.jersey</groupid>
    <artifactid>jersey-core</artifactid>
    <version>1.8</version>
</dependency>

score:0

the same error and wasted 2+ hours debugging and trying all options. i was not using the maven/pom, so i could not leverage that solution given by few.

finally the following resolved it: adding the jars directly to the tomcat/lib (not web-inf\lib) folder and restarting the tomcat.

score:0

if anyone is trying to build a hello world application using jersey, i think one of the easiest ways is to follow jersey documentation.

https://jersey.github.io/download.html

if you are already using maven, it'd take only a few minutes to see the result.

i used below.

mvn archetype:generate -darchetypegroupid=org.glassfish.jersey.archetypes -darchetypeartifactid=jersey-quickstart-webapp -darchetypeversion=2.26

score:0

it basically depends on which version jersey you are using. if you are using jersey ver.1.x.x you need to add

jersey 1 uses "com.sun.jersey", and jersey 2 uses org.glassfish. on servlet class tag. also, note that also init-param starting with com.sun.jersey won't be recognized by jersey 2.

and add all the jar file into web-inf lib folder

score:0

in my case, it worked after adding the jersey-bundle jar in my tomcat lib.

score:1

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

score:2

i had the same problem as you though i have followed a different guide: http://www.mkyong.com/webservices/jax-rs/jersey-hello-world-example/

the strange part is that, in this guide i have used, i should not have any problem with compatibility between versions (1.x against 2.x) because following the guide you use the jersey 1.8.x on pom.xmland in the web.xmlyou refer to a class (com.sun.jersey.spi.container.servlet.servletcontainer) as said before of 1.x version. so as i can infer this should be working.

my guess is because i'm using jdk 1.7 this class does not exist anymore.


after, i tried to resolve with the answers before mine, did not helped, i have made changes on the pom.xmland on the web.xml the error changed to: java.lang.classnotfoundexception: org.glassfish.jersey.servlet.servletcontainer

which supposedly should be exist!

as result of this error, i found a "new" solution: http://marek.potociar.net/2013/06/13/jax-rs-2-0-and-jersey-2-0-released/

with maven (archetypes), generate a jersey project, likes this:

mvn archetype:generate -darchetypegroupid=org.glassfish.jersey.archetypes -darchetypeartifactid=jersey-quickstart-webapp -darchetypeversion=2.0

and it worked for me! :)

score:3

try this :

org.glassfish.jersey.servlet.servletcontainer

on servlet-class

score:3

i also faced a similar issue. resolved the problem by going through the step step tutorial from the below link.

http://examples.javacodegeeks.com/enterprise-java/rest/jersey/jersey-hello-world-example/

  • the main thing to notice is that the jersey libraries should be placed correctly in tomcat web-inf/lib folder. it is done automatically by the eclipse settings mentioned in the above link. it will create a war file with the dependent jar files. else, you will run into problems with classnotfound exception.

apache-tomcat-7.0.56-windows-x64\apache -tomcat-7.0.56\webapps\jerseyjsonexample\web-inf\lib

"11/23/2014 12:06 am 130,458 jersey-client-1.9.jar

11/23/2014 12:06 am 458,739 jersey-core-1.9.jar

11/23/2014 12:06 am 147,952 jersey-json-1.9.jar

11/23/2014 12:06 am 713,089 jersey-server-1.9.jar" 4 file(s) 1,450,238 bytes

  • the second tutorial explains about how to create a webservice which produces and consumes json output.

http://examples.javacodegeeks.com/enterprise-java/rest/jersey/json-example-with-jersey-jackson/

both the links gave a good picture on how things work and save a lot of time.

score:15

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 -> gradle dependencies -> finish.

so eclipse wasn't using the gradle dependencies when apache was starting .

score:22

add this in pom

<dependency>
    <groupid>com.sun.jersey</groupid>
    <artifactid>jersey-server</artifactid>
    <version>1.17.1</version>
</dependency>
<dependency>
    <groupid>com.sun.jersey</groupid>
    <artifactid>jersey-core</artifactid>
    <version>1.17.1</version>
</dependency>
<dependency>
    <groupid>com.sun.jersey</groupid>
    <artifactid>jersey-servlet</artifactid>
    <version>1.17.1</version>
</dependency>

score:34

if you are using jersey 2.x then you need different configuration in web.xml as servlet class is change in it. you can update your web.xml with following configuration.

    <servlet>
    <servlet-name>myrest</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.servletcontainer</servlet-class>    
    <init-param>
      <param-name>jersey.config.server.provider.packages</param-name>
      <param-value>your.package.path</param-value>
    </init-param>
    <init-param>
     <param-name>unit:widgetpu</param-name>
     <param-value>persistence/widget</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>myrest</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>

Related Query

More Query from same tag