Bartłomiej Roguś
Bartłomiej Roguś

Reputation: 21

org.glassfish.jersey.servlet.ServletContainer ClassNotFoundException on server startup

I'm trying to run REST interface using Tomcat and Jersey with no success, I'm getting annoying error at server startup

Oct 13, 2014 2:53:45 PM org.apache.catalina.core.ApplicationContext log
INFO: Marking servlet Jersey Web Application as unavailable
Oct 13, 2014 2:53:45 PM org.apache.catalina.core.StandardContext loadOnStartup
SEVERE: Servlet  threw load() exception
java.lang.ClassNotFoundException: org.glassfish.jersey.servlet.ServletContainer
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
    at (...)

I'm using:

  1. OS X 10.8.5
  2. IntelliJ IDEA 13.1.5
  3. Tomcat 7.0.56
  4. Java JDK 7 Update 67
  5. Maven 3.0.4
  6. Jersey 2.13

My pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.javacodegeeks.enterprise.rest.jersey</groupId>
    <artifactId>JerseyJSONExample</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <repositories>
        <repository>
            <id>maven2-repository.java.net</id>
            <name>Java.net Repository for Maven</name>
            <url>http://download.java.net/maven/2/</url>
            <layout>default</layout>
        </repository>
    </repositories>

    <dependencies>

        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
            <version>2.13</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <!-- if your container implements Servlet API older than 3.0, use "jersey-container-servlet-core"  -->
            <artifactId>jersey-container-servlet</artifactId>
            <version>2.13</version>
        </dependency>

        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>2.0</version>
        </dependency>

        <!--<dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-json</artifactId>
            <version>1.18</version>
        </dependency>-->

    </dependencies>

</project>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <display-name>JAVA REST TEST</display-name>

    <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>RestApplication</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/api/*</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>

</web-app>

I've checked similar questions but all of them was solved by changing Jersey version, but looks like I'm using correct one.

List of libraries generated by maven:

Any ideas what I'm doing wrong?

Upvotes: 1

Views: 9637

Answers (2)

Harish Pathak
Harish Pathak

Reputation: 1607

You can face this exception if you are setting up jersey 2 project first time in eclipse using maven plugin. Message is simple enough to identify the root cause the Jersey libraries are not in classpath.

Solution – Add Jersey Library in Deployment Assembly

  • Open your project’s deployment assembly configuration.

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

  • Deployment Assembly - Add Build Path Entries

  • Select Maven Dependencies

Now when you again run the project after building it, this will run fine and will be able to find org.glassfish.jersey.servlet.ServletContainer class.

Upvotes: 1

Bartłomiej Roguś
Bartłomiej Roguś

Reputation: 21

Ok, found what was wrong. In my case I've added lib folder to WEB-INF (Project Structure->Artifacts, then in "Available Elements" select all of them, right click and select "Put into /WEB-INF/lib").

Upvotes: 1

Related Questions