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

I just made my first REST api based on this http://www.vogella.com/articles/REST/article.html I use eclipse kepler, tomcat7 and Jersey

when I try "Run on server", I get this error:

SEVERE: Servlet /de.vogella.jersey.first threw load() exception java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559) at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:527) at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:509) at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:137) at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1144) at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088) at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5176) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5460) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549) at java.util.concurrent.FutureTask.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source) 

Here is the source code:

 package de.vogella.jersey.first; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; @Path("/hello") public class Hello { // This method is called if TEXT_PLAIN is request @GET @Produces(MediaType.TEXT_PLAIN) public String sayPlainTextHello() { return "Hello Jersey"; } // This method is called if XML is request @GET @Produces(MediaType.TEXT_XML) public String sayXMLHello() { return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>"; } // This method is called if HTML is request @GET @Produces(MediaType.TEXT_HTML) public String sayHtmlHello() { return "<html> " + "<title>" + "Hello Jersey" + "</title>" + "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> "; } } 

here is 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>de.vogella.jersey.first</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>de.vogella.jersey.first</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> 

here are the libraries i added from jersey:

 asm-all-repackaged-2.2.0-b14.jar cglib-2.2.0-b14.jar guava-14.0.1.jar hk2-api-2.2.0-b14.jar hk2-locator-2.2.0-b14.jar hk2-utils-2.2.0-b14.jar javax.annotation-api-1.2.jar javax.inject-2.2.0-b14.jar javax.servlet-api-3.0.1.jar javax.ws.rs-api-2.0.jar jaxb-api-2.2.7.jar jersey-client.jar jersey-common.jar jersey-container-servlet-core.jar jersey-container-servlet.jar jersey-server.jar org.osgi.core-4.2.0.jar osgi-resource-locator-1.0.1.jar persistence-api-1.0.jar validation-api-1.1.0.Final.jar 
+7
rest tomcat jersey
source share
4 answers

If you are using the jersey 2.x API, you need to rename the classes as described below and your problem will be solved.

 <servlet> <servlet-name>Jersey REST Service</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>org.glassfish.jersey.config.property.packages</param-name> <param-value>com.practise.rest</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> 

Karthik [aka DjNickZ]

+6
source share

You get

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

This means that you are using a different version of jar . You added jersey-server.jar , this jar updated to jersey-servlet-1.17.jar .

So add jersey-servlet-1.17.jar to your project.

+2
source share

We get this error due to a build path error. You must add the Runtime Server libraries to the Build Path.

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

To resolve a class exception not found, follow these steps.

Right-click on the project → Build Path → Java Build Path → Add Library → Server Runtime → Apache Tomcat v7.0

Thanks, Sachin GN

+1
source share

You need to add the following jar files to the web-inf / lib folder, you also need to configure the build path - right-click your project - Build path - configure the build path - libraries - add external banks (add all the jar files below). And restart the server

Jersey libs

0
source share

All Articles