Java org.glassfish.jersey.servlet.ServletContainer ClassNotFoundException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22022114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 13:07:22  来源:igfitidea点击:

org.glassfish.jersey.servlet.ServletContainer ClassNotFoundException

javatomcatjerseyglassfishclassnotfoundexception

提问by ssankara73

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

我一直收到 ClassNotFoundException 异常,org.glassfish.jersey.servlet.ServletContainer但它在昨晚我尝试使用 Eclipse Juno 启动/重新启动我的 Tomcat 服务器 (v7) 时特别开始。

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

这是我的 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:

我确保 jersey 的 jar 文件仍然存在于 WEB-INF/lib 中:

WEB-INF/lib with jersey jars

带有球衣罐的 WEB-INF/lib

采纳答案by unwichtich

The problem:

问题:

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

indicates that you try to use the Jersey 2.xservlet, but you are supplying the Jersey 1.xlibs.

表示您尝试使用Jersey 2.xservlet,但您提供的是Jersey 1.x库。

For Jersey 1.xyou have to do it like this:

对于Jersey 1.x,您必须这样做:

<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.xthen you'll have to supply the Jersey 2.xlibs. In a maven based project you can use the following:

有关更多信息,请查看Jersey 1.x 文档

如果您想使用Jersey 2.x,则必须提供Jersey 2.x库。在基于 Maven 的项目中,您可以使用以下内容:

<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.xyou don't need to setup anything in your web.xml, it is sufficient to supply a class similar to this:

对于Jersey 2.x,您不需要在您的web.xml.

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.

有关更多信息,请查看Jersey 文档

See also:

也可以看看:

回答by agpt

If you are using Jersey 2.xuse following dependency:

如果您使用的是Jersey 2.x,请使用以下依赖项:

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

Where XXcould be any particular version you look for. Jersey Containers.

XX您要查找的任何特定版本在哪里。泽西集装箱

回答by Lalit Kale

Below code work for me in web.xml file

下面的代码在 web.xml 文件中对我有用

<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>

回答by Adi

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

这是日食设置问题,而不是泽西岛问题。

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

从这个线程 ClassNotFoundException: org.glassfish.jersey.servlet.ServletContainer

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

右键单击您的 eclipse 项目 Properties -> Deployment Assembly -> Add -> Java Build Path Entries -> Gradle Dependencies -> Finish。

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

所以 Apache 启动时 Eclipse 没有使用 Gradle 依赖项。

回答by Christopher Kikoti

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

假设您使用的是 Jersey 2.25.1,这对我有用 - 我正在使用 Apache Tomcat Web 容器:

    <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

注意:用您正在使用的版本替换版本

回答by asoni94

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 名称:-

<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!

因此,在我的情况下,删除 .class 工作正常。希望它会帮助某人!

回答by 15412s

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

我在eclipse中遇到了同样的问题,WA解决方案是将libs复制到WEB-INF/lib

回答by bvdb

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

jersey-container-servlet实际使用的jersey-container-servlet-core依赖。但是,如果您使用 maven,那并不重要。如果您只是定义jersey-container-servlet用法,它也会自动下载依赖项。

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.ServletContainerclass is actually part of the core dependency.

但是对于那些手动将 jar 文件添加到他们的项目(即没有 maven)的人来说,重要的是要知道您实际上需要这两个 jar 文件。本org.glassfish.jersey.servlet.ServletContainer类实际上是核心依赖的一部分。

回答by Jordi M.

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

如果你不使用 maven,试着把你的 jars 放到 WEB-INF/lib,它对我有用。