Search code examples
javaservletsjerseyjava-ee-8

Class not found even though it is clearly visible in Web App Libraries


Similar question exists but none of the answers helped.

Starting Tomcat Server 9 on my Eclipse Project gives the error

java.lang.ClassNotFoundException: org.glassfish.jersey.servlet.ServletContainer.class
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1364)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1187)
    at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:539)

Even though I can clearly see ServletContainer.class under org.glassfish.jersey.servlet under jersey-container-servlet-core.jar under Web App Libraries.

All the required jars were added to /src/main/webapp/WEB-INF/lib, they seem to be properly imported.

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>JavaAPI</display-name>
  
    <servlet>
        <servlet-name>JAVA API</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer.class</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>test</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup> <!-- Tried Removing this, no difference -->
    </servlet>
    <servlet-mapping>
        <servlet-name>JAVA API</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

Hello.java:

package test;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@Path("/hello")
public class Hello {
    
    @GET
    @Produces(MediaType.TEXT_XML)
    public String sayHello() {
        String resource = "<? xml version='1.0' ?>" + 
                    "<hello>Hi Varun! This is the sayHello call.</hello>";
        return resource;
    }
    
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public String sayHelloJSON() {
        String resource = null;
        return resource;
    }
    
    @GET
    @Produces(MediaType.TEXT_HTML)
    public String sayHelloHTML() {
        String resource = "<h1>Hi Varun! This is the sayHelloHTML call.</h1>";
        return resource;
    }
}

Project Structure:
Project Structure

Using:

  • Eclipse: 2021-03
  • Tomcat: 9
  • Java: 1.8 (set under BuildPath, ProjectFacets)
  • JAX-RS 2.0 / Jersey 2.25.x

No Maven, Gradle involved, simply web project, with every jar hand-picked and imported.

Was following this tutorial step-by-step.


Solution

  • Remove the .class suffix

    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    

    instead of

    <servlet-class>org.glassfish.jersey.servlet.ServletContainer.class</servlet-class>
    

    You specifiy the class name, not the class object.