Tom
Tom

Reputation: 393

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?

Upvotes: 14

Views: 49824

Answers (10)

Varun
Varun

Reputation: 4452

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.

Upvotes: 2

Miniman
Miniman

Reputation: 241

I would like to add, I had the same problem and @Edin's answer worked, however I was using IntelliJ. So I thought I would add a way to do this for Intellij (for future people like me):

  1. File
  2. Project Structure
  3. Artifacts
  4. You should have an Artifact there named after your project, select it
  5. On the right, you will see a list Available Elements
  6. Select all the available elements, right click, then select the option that adds them to the WEB-INF/Lib

Upvotes: 0

Mohit Singh
Mohit Singh

Reputation: 6167

Add Jersey library to the build path under library

Upvotes: 0

kaya
kaya

Reputation: 1666

For me, the fix was doing this:

Right click on Project -> Properties -> Java Build Path -> Libraries ->
Add Library... -> Server Runtime -> Apache Tomcat v7.0 -> Finish

Another step i did was to import stuff in Maven but it wasnt neccessary as it is running fine without it too.

But here it is if it helps anyone

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.2.1-b03</version>
        <scope>provided</scope>
    </dependency>

putting servlet jars in WEB-INF/lib just did things more complicated as the Exception Message got more complicated and irrelevant

EDIT: oh what also does help is to do an Update Project on your Maven Project.

Upvotes: 1

Damon Smith
Damon Smith

Reputation: 1790

I got this problem with Eclipse. My project which has a very similar setup - Jersey 2, Tomcat 7. I'm running tomcat from inside eclipse, and it seems that somewhere along the way eclipse sort of forgot how to deploy the libraries to tomcat properly.

I tried deploying my war file manually to tomcat and starting it via startup.sh and it worked fine, but if I start it up via eclipse I get my web content but the Jersey classes aren't there so the REST API doesn't work. My login servlet works fine, just not the jersey bit. The console has the same error:

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

on startup. So it must be a problem with how eclipse deploys code to tomcat. I tried going into eclipse servers and clearing the work dir, didn't do anything. (not that it would, for missing classes).

The only thing that worked was going to my Servers config in Eclipse and deleting my tomcat entry then creating a new one. I did that and it fixed it straight away.

Upvotes: 1

saravana kumaran
saravana kumaran

Reputation: 1

Putting the jersey jar files in WEB-INF/lib should work.

If you're not able to put your jersey jar files in WEB-INF/lib, try copy pasting it manually by going through your workspace and restart eclipse ! That worked for me.

Upvotes: 0

Devil
Devil

Reputation: 291

I am also observing the same issue. I am using tomcat8 with Maven. If I build Dynamic Web Project and add all libraries manually, project works fine but if I use Maven, it gives following error:

SEVERE: Servlet [Jersey REST Service] in web application [/EmployeeManagement] threw load() exception
java.lang.ClassNotFoundException: org.glassfish.jersey.servlet.ServletContainer
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1313)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1164)
    at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:520)
    at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:501)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:120)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1095)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1031)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4914)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5201)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)

I have added following maven dependencies in pom.xml file:

<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-server</artifactId>
    <version>2.21</version>
</dependency>
    <dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet-core</artifactId>
    <version>2.21</version>
</dependency>

Upvotes: 0

Pantelis Natsiavas
Pantelis Natsiavas

Reputation: 5369

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>

Upvotes: 8

Ebin
Ebin

Reputation: 201

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.

Upvotes: 20

betteroutthanin
betteroutthanin

Reputation: 7546

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.

Upvotes: 5

Related Questions