java.lang.ClassNotFoundException: org.glassfish.jersey.servlet.ServletContainer
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26444790/
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
java.lang.ClassNotFoundException: org.glassfish.jersey.servlet.ServletContainer
提问by Tom
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
:
您可能会觉得这是一个重复的问题,但没有一个具有相同标题的问题能解决我的问题。我正在使用 Jersey 2.0 在 Eclipse 中创建一个 RESTful Web 服务,我使用 Tomcat 7.0 作为我的服务器,我有以下内容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:
我有一个名为 Hello 的简单类:
@Path("customers")
public class Hello {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getCustomer() {
return "hello";
}
}
I have a Jersey library called jersey
:
我有一个泽西图书馆,名为jersey
:
Every time I ran this project, I got error of
每次我运行这个项目时,我都会出错
java.lang.ClassNotFoundException: org.glassfish.jersey.servlet.ServletContainer
Any ideas?
有任何想法吗?
采纳答案by Zhenxiao Hao
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.
等等,您说您有一个名为 的 Jersey 库jersey
,您创建User Library
并命名了jersey
吗?如果是这样,请尝试将球衣 jar 文件复制到WebContent -> WEB-INF -> lib 中。
回答by Damon Smith
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.
我在 Eclipse 中遇到了这个问题。我的项目具有非常相似的设置 - Jersey 2
, Tomcat 7
. 我正在从 eclipse 内部运行 tomcat,而且似乎在 eclipse 的某个地方忘记了如何正确地将库部署到 tomcat。
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:
我尝试将我的 war 文件手动部署到 tomcat 并通过startup.sh
它启动,它工作正常,但是如果我通过 eclipse 启动它,我会得到我的 Web 内容,但 Jersey 类不存在,因此 REST API 不起作用。我的登录 servlet 工作正常,只是不是球衣部分。控制台有同样的错误:
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).
在启动时。所以肯定是eclipse如何部署代码到tomcat的问题。我尝试进入 eclipse 服务器并清除工作目录,但没有做任何事情。(并非如此,对于缺少的课程)。
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.
唯一有效的方法是在 Eclipse 中访问我的服务器配置并删除我的 tomcat 条目,然后创建一个新条目。我这样做了,它立即修复了它。
回答by Ebin
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:
对于 Eclipse 中的 Web 项目,几乎总是建议使用 Maven 管理依赖项。使用像 m2e 这样的 Maven 插件。向 Maven 项目添加依赖项后,它应该自动将这些库部署到WEB-INF/lib。如果没有(无论出于何种原因),您可以明确这样做:
- Right-click on your project in Project Explorer
- Select Properties
- Select Deployment Assembly
- If Maven Dependenciesis not one of the entries, the libraries were not added automatically, so we'll add them now
- Select Add
- Select Java Build Path Entries
- Select Maven Dependencies
- Click Finish
- 在Project Explorer 中右键单击您的项目
- 选择属性
- 选择部署程序集
- 如果Maven Dependencies不是条目之一,则库不会自动添加,因此我们现在将添加它们
- 选择添加
- 选择Java 构建路径条目
- 选择Maven 依赖项
- 单击完成
This should add the libraries to WEB-INF/lib, although you'd still not see them in the Project Explorer view. But your ClassNotFoundExceptionshould go away now.
这应该将库添加到WEB-INF/lib,尽管您仍然不会在 Project Explorer 视图中看到它们。但是您的ClassNotFoundException现在应该消失了。
回答by Pantelis Natsiavas
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:
虽然以上所有内容都适用(Eclipse 不是最可靠的应用程序),但请确保您已仔细检查您的 Maven 依赖项。您应该注意到,对于 jersey 2.19,我必须添加以下 2 个 maven 依赖项:
<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>
回答by Devil
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:
我也在观察同样的问题。我在 Maven 中使用 tomcat8。如果我构建动态 Web 项目并手动添加所有库,项目工作正常,但如果我使用 Maven,则会出现以下错误:
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:
我在 pom.xml 文件中添加了以下 maven 依赖项:
<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>
回答by saravana kumaran
Putting the jersey jarfiles in WEB-INF/libshould work.
将球衣 jar文件放在WEB-INF/lib 中应该可以工作。
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.
如果您无法将 jersey jar 文件放在WEB-INF/lib 中,请尝试通过您的工作区手动复制粘贴它并重新启动 eclipse !那对我有用。
回答by kaya
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.
我做的另一个步骤是在 Maven 中导入东西,但这不是必需的,因为它在没有它的情况下也能正常运行。
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
将 servlet jars 放在 WEB-INF/lib 中只是做了更复杂的事情,因为异常消息变得更加复杂和不相关
EDIT: oh what also does help is to do an Update Project on your Maven Project.
编辑:哦还有帮助的是在你的 Maven 项目上做一个更新项目。
回答by Mohit Singh
Add Jersey library to the build path under library
将 Jersey 库添加到 library 下的构建路径
回答by Miniman
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):
我想补充一点,我遇到了同样的问题,@Edin 的回答有效,但我使用的是IntelliJ。所以我想我会为 Intellij 添加一种方法来做到这一点(对于像我这样的未来人):
- File
- Project Structure
- Artifacts
- You should have an Artifact there named after your project, select it
- On the right, you will see a list Available Elements
- Select all the available elements, right click, then select the option that adds them to the WEB-INF/Lib
- 文件
- 项目结构
- 文物
- 你应该有一个以你的项目命名的工件,选择它
- 在右侧,您将看到可用元素列表
- 选择所有可用元素,右键单击,然后选择将它们添加到WEB-INF/Lib 的选项
回答by Varun
Message is simple enough to identify the root cause the Jersey libraries are not in classpath.
消息很简单,可以确定 Jersey 库不在类路径中的根本原因。
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.
您应该首先检查天气是否在 pom.xml 中定义了球衣的依赖项。如果不是这里是您可以定义的依赖项。如果它已经在 pom.xml 中定义,请忽略在 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
解决方案——在部署程序集中添加 Jersey 库
1. Right-click on your project in Project Explorer
1. 在 Project Explorer 中右键单击您的项目
2. Open your project's deployment assembly configuration.
2. 打开您项目的部署程序集配置。
3. Add Build path jar files in assembly so that they can be added to lib folder in final war file.
3.在程序集中添加构建路径jar文件,以便它们可以添加到最终war文件中的lib文件夹中。
4. Updated assembly will look like this.
4. 更新后的程序集将如下所示。
Click ok. Now you are good to go.
单击确定。现在你可以走了。