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

EclipseGoogle App-EngineJakarta EeJerseyJersey 2.0

Eclipse Problem Overview


I am trying to build a simple hello world application for two days using Jersey + Google app engine. For simple AppEngine project I followed these tutorials and both works just fine https://developers.google.com/appengine/docs/java/gettingstarted/creating https://developers.google.com/appengine/docs/java/webtoolsplatform

But now I am trying to add Jersey and following this tutorial http://www.vogella.com/articles/REST/article.html.

But server keeps giving me

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

when I add these lines in web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>TestServer</display-name>
<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>
<servlet-mapping>
	<servlet-name>Jersey REST Service</servlet-name>
	<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>

I have downloaded Jersey JAX-RS 2.1 RI bundle from here and have added all jar files in WEB-INF/lib folder as described in tutorial. And even after two days nothing is working. I have searched several times on Google and apparently people who are using Maven have solved it somehow but I am not using Maven neither did the guy who wrote that tutorial.

Just to check if even com.sun.jersey.spi.container.servlet.ServletContainer exists in imported Jersey jars I tried to just write this fully qualified name in Java and let the intellisense finish names but I couldn't get any intellisense after com.sun.je so my last guess is that there have been some package rearrangement in latest Jersey build and jersey is no longer inside com.sun. I am exhausted and I would appreciate any kind of help.

Eclipse Solutions


Solution 1 - Eclipse

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.

Solution 2 - Eclipse

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>

Solution 3 - Eclipse

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>

Solution 4 - Eclipse

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 .

Solution 5 - Eclipse

try this :

> org.glassfish.jersey.servlet.ServletContainer

on servlet-class

Solution 6 - Eclipse

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.

Solution 7 - Eclipse

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! :)

Solution 8 - Eclipse

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

Solution 9 - Eclipse

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.

Solution 10 - Eclipse

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>

Solution 11 - Eclipse

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

Solution 12 - Eclipse

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.

Solution 13 - Eclipse

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.

Solution 14 - Eclipse

In pom.xml file we need to add

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

Solution 15 - Eclipse

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.

Solution 16 - Eclipse

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

Solution 17 - Eclipse

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

Solution 18 - Eclipse

These might be your dependencies mixed with version and make sure these dependecies are add in pom file

<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>

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

Now check Once check Servlet mapping part is correct.

References : RESTful Web Services with using Jersey

<servlet>
<servlet-name>RestServices</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.javaproficiency.jerseydemo.demo</param-value>
</init-param>

<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>RestServices</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>

Solution 19 - Eclipse

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionM-WajeehView Question on Stackoverflow
Solution 1 - EclipseMichal GajdosView Answer on Stackoverflow
Solution 2 - Eclipsep27View Answer on Stackoverflow
Solution 3 - Eclipseuser1249655View Answer on Stackoverflow
Solution 4 - EclipseAdiView Answer on Stackoverflow
Solution 5 - EclipsePichitronView Answer on Stackoverflow
Solution 6 - EclipseyogishajView Answer on Stackoverflow
Solution 7 - EclipsePaulo OliveiraView Answer on Stackoverflow
Solution 8 - EclipseSachin G NView Answer on Stackoverflow
Solution 9 - EclipserickygrimesView Answer on Stackoverflow
Solution 10 - EclipseAhmad R. NazemiView Answer on Stackoverflow
Solution 11 - EclipseJuan David Grisales GarzonView Answer on Stackoverflow
Solution 12 - EclipseshineView Answer on Stackoverflow
Solution 13 - EclipseAndy1625View Answer on Stackoverflow
Solution 14 - EclipseKARTHIKEYAN.AView Answer on Stackoverflow
Solution 15 - EclipseVeluView Answer on Stackoverflow
Solution 16 - EclipseRangaView Answer on Stackoverflow
Solution 17 - EclipseArunView Answer on Stackoverflow
Solution 18 - EclipseAnuj DhimanView Answer on Stackoverflow
Solution 19 - EclipseRaghul SubramaniamView Answer on Stackoverflow