Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.ClassNotFoundException: org.glassfish.jersey.servlet.ServletContainer

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:

enter image description here

Every time I ran this project, I got error of

java.lang.ClassNotFoundException: org.glassfish.jersey.servlet.ServletContainer

Any ideas?

like image 424
Tom Avatar asked Oct 18 '14 21:10

Tom


4 Answers

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>
like image 200
Pantelis Natsiavas Avatar answered Nov 20 '22 06:11

Pantelis Natsiavas


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.

like image 30
betteroutthanin Avatar answered Nov 20 '22 05:11

betteroutthanin


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:

  1. Right-click on your project in Project Explorer
  2. Select Properties
  3. Select Deployment Assembly
  4. If Maven Dependencies is not one of the entries, the libraries were not added automatically, so we'll add them now
  5. Select Add
  6. Select Java Build Path Entries
  7. Select Maven Dependencies
  8. Click Finish

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.

like image 20
Ebin Avatar answered Nov 20 '22 05:11

Ebin


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.

enter image description here

3. Add Build path jar files in assembly so that they can be added to lib folder in final war file.

enter image description here

4. Updated assembly will look like this.

enter image description here

Click ok. Now you are good to go.

like image 2
Varun Avatar answered Nov 20 '22 04:11

Varun