org.glassfish.jersey.servlet.ServletContainer ClassNotFoundException
Asked Answered
K

10

57

I have been getting a ClassNotFoundException with org.glassfish.jersey.servlet.ServletContainer but it peculiarly started last night when I tried to start/re-start my Tomcat server (v7) with Eclipse Juno.

Not sure what is going on. What's bizarre is that it just started happening last night while it was working perfectly well before that.

Here is the stacktrace:

Feb 25, 2014 11:11:19 AM org.apache.catalina.core.ApplicationContext log
INFO: Marking servlet com.att.ucomm.admin.UCommAdminFunctions as unavailable
Feb 25, 2014 11:11:19 AM org.apache.catalina.core.StandardContext loadOnStartup
SEVERE: Servlet /UCommAdminFunctions threw load() exception
java.lang.ClassNotFoundException: org.glassfish.jersey.servlet.ServletContainer
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1671)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1516)
at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:415)
at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:397)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:118    )
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1048)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:996)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4762)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5045)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:140)
at org.apache.catalina.core.StandardContext.reload(StandardContext.java:3670)
at org.apache.catalina.loader.WebappLoader.backgroundProcess(WebappLoader.java:424)
at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1207)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1393)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1403)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1403)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1382)
at java.lang.Thread.run(Unknown Source)

Feb 25, 2014 11:11:19 AM org.apache.catalina.core.StandardContext reload
INFO: Reloading Context with name [/UCommAdminFunctions] is completed
Feb 25, 2014 11:13:33 AM org.apache.catalina.core.StandardWrapperValve invoke
INFO: Servlet com.att.ucomm.admin.UCommAdminFunctions is currently unavailable

Here is my web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<servlet>
    <servlet-name>com.att.ucomm.admin.UCommAdminFunctions</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>

    <!-- Register JAX-RS Application, if needed. -->
    <init-param>
        <param-name>com.att.ucomm.admin.UCommAdminFunctions</param-name>
        <param-value>my.package.MyApplication</param-value>
    </init-param>

    <!-- Register resources and providers under my.package. -->
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.att.ucomm.admin</param-value>
    </init-param>

    <!-- Register my custom provider (not needed if it's in my.package) AND LoggingFilter. -->
    <init-param>
        <param-name>jersey.config.server.provider.classnames</param-name>
        <param-value>com.att.ucomm.admin.SecurityRequestFilter;org.glassfish.jersey.filter.LoggingFilter</param-value>
    </init-param>

    <!-- Enable Tracing support. -->
    <init-param>
        <param-name>jersey.config.server.tracing</param-name>
        <param-value>ALL</param-value>
    </init-param>

    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>com.att.ucomm.admin.UCommAdminFunctions</servlet-name>
    <url-pattern>/rest/*</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 made sure that the jar files for jersey were still there in WEB-INF/lib:

WEB-INF/lib with jersey jars

Kass answered 25/2, 2014 at 17:47 Comment(1)
If someone encounters the same problem after a project renaming ... 1. Project > Clean... 2. Project > Build AllBeauvais
G
96

The problem:

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

indicates that you try to use the Jersey 2.x servlet, but you are supplying the Jersey 1.x libs.

For Jersey 1.x you have to do it like this:

<servlet>
  <servlet-name>Jersey REST Service</servlet-name>
<servlet-class>
  com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
  <init-param>
    <param-name>com.sun.jersey.config.property.packages</param-name>
    <param-value>sample.hello.resources</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>Jersey REST Service</servlet-name>
  <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

For more information check the Jersey 1.x documentation.

If you instead want to use Jersey 2.x then you'll have to supply the Jersey 2.x libs. In a maven based project you can use the following:

<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet</artifactId>
    <version>2.xx</version>
</dependency>
<!-- if you are using Jersey client specific features without the server side -->
<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-client</artifactId>
    <version>2.xx</version>
</dependency>

For Jersey 2.x you don't need to setup anything in your web.xml, it is sufficient to supply a class similar to this:

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

@ApplicationPath("rest")
public class ApplicationConfig extends Application {

}

For more information, check the Jersey documentation.

See also:

Gaily answered 25/2, 2014 at 19:16 Comment(6)
And what if you want to use Jersey 2.x ?Spontaneous
Whether you use Jersey 1 or 2, you still need dependencies (though the old ones are com.sun...). You only show a web.xml for Jersey 1.Substance
For Jersey 2 the servlet-class should be <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> in web.xml according to the docs, but I'm not sure what the param-name should be. jersey.java.net/documentation/latest/deployment.html . Update I think it's jersey.config.server.provider.packages.Enate
I am using Jersey 2.26, and Tomcat 8.0 with Jdk 8. But I am still getting the error, I have included all the jars to the build path. I am not using Maven, please give some solution without using Maven.Hulburt
Perfect explanation. Cheers.Mcavoy
After an afternoon spent around tutorials and docs, THIS resolved my issue with Jetty and JAX-RS 2.0- Thanks.Vocation
D
26

It's an eclipse setup issue, not a Jersey issue.

From this thread ClassNotFoundException: org.glassfish.jersey.servlet.ServletContainer

Right click your eclipse project Properties -> Deployment Assembly -> Add -> Java Build Path Entries -> Gradle Dependencies -> Finish.

So Eclipse wasn't using the Gradle dependencies when Apache was starting .

Disgruntle answered 4/8, 2016 at 19:0 Comment(3)
This should be the accepted answer. None of the other answers actually answer the question legitimately.Expediential
This was the answer I was looking for (which fixed my problem). Thanks!Yetta
After Java Build Path Entries there are no entries, nothing to select.Innovation
Y
12

If you are using Jersey 2.x use following dependency:

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

Where XX could be any particular version you look for. Jersey Containers.

Yoheaveho answered 27/12, 2014 at 16:11 Comment(0)
D
7

I agree with the accepted answer. But for me, the issue was not that, instead I had to modify my Servlet-Class name from:-

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

To:

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

So, removing .class worked fine in my case. Hope it will help somebody!

Disini answered 25/11, 2017 at 21:18 Comment(0)
Q
5

Below code work for me in web.xml file

<servlet>
    <servlet-name>WebService</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.example.demo.webservice</param-value>
        //Package
    </init-param>
    <init-param>
        <param-name>unit:WidgetPU</param-name>
        <param-value>persistence/widget</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>WebService</servlet-name>
    <url-pattern>/webservices/*</url-pattern>
</servlet-mapping>
Quixotism answered 22/9, 2015 at 10:50 Comment(0)
C
1

The jersey-container-servlet actually uses the jersey-container-servlet-core dependency. But if you use maven, that does not really matter. If you just define the jersey-container-servlet usage, it will automatically download the dependency as well.

But for those who add jar files to their project manually (i.e. without maven) It is important to know that you actually need both jar files. The org.glassfish.jersey.servlet.ServletContainer class is actually part of the core dependency.

Cardenas answered 10/7, 2018 at 15:51 Comment(1)
I don't use Maven, I use Ant. In my humble opinion, your answer is incomplete. Yes, both JARs are needed but they aren't enough, you need jersey-server.jar too. Otherwise, you get java.lang.ClassNotFoundException: org.glassfish.jersey.server.spi.ContainerTabethatabib
L
1

If you not use maven, try to put your jars to WEB-INF/lib, it worked for me.

Lenz answered 28/8, 2018 at 15:24 Comment(0)
L
0

Suppose you are usin Jersey 2.25.1, this worked for me - I am using Apache Tomcat web container:

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

NB: Replace version with the version you are using

Lewislewisite answered 14/3, 2017 at 8:44 Comment(0)
R
0

I had the same problem with eclipse, the WA solution was to copy the libs to WEB-INF/lib

Reputed answered 1/2, 2018 at 22:25 Comment(0)
R
0

For me it seems like the issue was Eclipse not recognizing the Maven dependencies of my project.

What I did:

Right click on project > Configure > Convert to Maven Project

Rabah answered 21/2, 2022 at 23:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.