You may feel this is a duplicated question, but none of the questions with the same title solve my problems. I am using Jersey 2.0 creating a RESTful web service in Eclipse, I use Tomcat 7.0 as my server, I have the following 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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<servlet>
<servlet-name>JAX-RS Servlet</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.shop.domain</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JAX-RS Servlet</servlet-name>
<url-pattern>/jaxrs/*</url-pattern>
</servlet-mapping>
</web-app>
I have a simple class called Hello:
@Path("customers")
public class Hello {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getCustomer() {
return "hello";
}
}
I have a Jersey library called jersey
:
Every time I ran this project, I got error of
java.lang.ClassNotFoundException: org.glassfish.jersey.servlet.ServletContainer
Any ideas?
While all of the above could apply (Eclipse is not the most credible application) make sure that you have double checked your maven dependencies. You should note that for jersey 2.19 I had to add these 2 maven dependencies:
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.19</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.19</version>
</dependency>
Wait, you said you have a Jersey library called jersey
, did you create a User Library
and name it jersey
? If so, try to copy your jersey jar files into WebContent -> WEB-INF -> lib.
I guess this has already been answered, but just wanted to add an extra tip.
For web projects in Eclipse, it is almost always advisable to manage dependencies with Maven. Use a Maven plugin like m2e. Once you add a dependency to your Maven project, it should automatically deploy those libraries to WEB-INF/lib. If it does not (for whatever reasons), you can explicit do so:
This should add the libraries to WEB-INF/lib, although you'd still not see them in the Project Explorer view. But your ClassNotFoundException should go away now.
Message is simple enough to identify the root cause the Jersey libraries are not in classpath.
The first thing you should check that weather do you have the dependencies for jersey defined in you pom.xml or not. if not here are the dependencies you can define. if it is already defined in pom.xml, ignore adding dependencies in your pom.xml.
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.19</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.19</version>
</dependency>
Solution – Add Jersey Library in Deployment Assembly
1. Right-click on your project in Project Explorer
2. Open your project’s deployment assembly configuration.
3. Add Build path jar files in assembly so that they can be added to lib folder in final war file.
4. Updated assembly will look like this.
Click ok. Now you are good to go.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With