eclipse java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18086218/
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-09-10 21:50:48  来源:igfitidea点击:

java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer

eclipsegoogle-app-enginejakarta-eejerseyjersey-2.0

提问by M-WaJeEh

I am trying to build a simple hello world application for two days using Jersey + Google app engine. For simple AppEngine project I followed these tutorials and both works just fine https://developers.google.com/appengine/docs/java/gettingstarted/creatinghttps://developers.google.com/appengine/docs/java/webtoolsplatform

我正在尝试使用 Jersey + Google 应用引擎构建一个简单的 hello world 应用程序,为期两天。对于简单的 AppEngine 项目,我遵循了这些教程,两者都运行良好 https://developers.google.com/appengine/docs/java/gettingstarted/creating https://developers.google.com/appengine/docs/java/webtoolsplatform

But now I am trying to add Jersey and following this tutorial http://www.vogella.com/articles/REST/article.html.

但现在我正在尝试添加 Jersey 并遵循本教程http://www.vogella.com/articles/REST/article.html

But server keeps giving me

但是服务器一直给我

java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer

java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer

when I add these lines in web.xml:

当我在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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>TestServer</display-name>
<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>com.test.myproject</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>
</web-app>

I have downloaded Jersey JAX-RS 2.1 RI bundlefrom hereand have added all jar files in WEB-INF/libfolder as described in tutorial. And even after two days nothing is working. I have searched several times on Google and apparently people who are using Maven have solved it somehow but I am not using Maven neither did the guy who wrote that tutorial.

我已经JAX-RS 2.1 RI bundle这里下载了 Jersey并WEB-INF/lib按照教程中的描述在文件夹中添加了所有 jar 文件。即使两天后也没有任何效果。我在谷歌上搜索了几次,显然使用 Maven 的人已经以某种方式解决了它,但我没有使用 Maven,编写该教程的人也没有使用。

Just to check if even com.sun.jersey.spi.container.servlet.ServletContainerexists in imported Jersey jarsI tried to just write this fully qualified name in Java and let the intellisense finish names but I couldn't get any intellisense after com.sun.jeso my last guess is that there have been some package rearrangementin latest Jersey build and jerseyis no longer inside com.sun. I am exhausted and I would appreciate any kind of help.

只是为了检查com.sun.jersey.spi.container.servlet.ServletContainer导入中是否存在 Jersey jars我试图只用Java编写这个完全限定的名称并让智能感知完成名称但之后我无法获得任何智能感知com.sun.je所以我最后的猜测是在最新的Jersey构建中进行了一些包重新排列并且jersey不再在里面com.sun。我已经筋疲力尽了,如果能得到任何帮助,我将不胜感激。

回答by Michal Gajdos

You have downloaded Jersey 2 (which RI of JAX-RS 2). The tutorial you're referring to uses Jersey 1. Download Jersey 1.17.1 from (here), should be sufficient for you.

您已下载 Jersey 2(JAX-RS 2 的 RI)。您所指的教程使用 Jersey 1。从(此处)下载 Jersey 1.17.1 ,对您来说应该足够了。

Jersey 1 uses com.sun.jersey, and Jersey 2 uses org.glassfish.jerseyhence the exception.

Jersey 1 使用com.sun.jersey,而 Jersey 2 使用org.glassfish.jersey因此例外。

Also note that also init-paramstarting with com.sun.jerseywon't be recognized by Jersey 2.

另请注意,以init-param开头com.sun.jersey也不会被 Jersey 2 识别。

Edit

编辑

Registering Resources and Providers in Jersey 2contains additional info on how to register classes/instances in Jersey 2.

在 Jersey 2注册资源和提供程序包含有关如何在 Jersey 2中注册类/实例的附加信息。

回答by p27

If you are using jersey 2.x then you need different configuration in web.xml as servlet class is change in it. you can update your web.xml with following configuration.

如果您使用的是 jersey 2.x,那么您需要在 web.xml 中进行不同的配置,因为 servlet 类在其中发生了变化。您可以使用以下配置更新您的 web.xml。

    <servlet>
    <servlet-name>myrest</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>    
    <init-param>
      <param-name>jersey.config.server.provider.packages</param-name>
      <param-value>your.package.path</param-value>
    </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>myrest</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>

回答by user1249655

Add this in pom

在pom中添加这个

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-server</artifactId>
    <version>1.17.1</version>
</dependency>
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-core</artifactId>
    <version>1.17.1</version>
</dependency>
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-servlet</artifactId>
    <version>1.17.1</version>
</dependency>

回答by Adi

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

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

From this thread ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer

从这个线程 ClassNotFoundException: com.sun.jersey.spi.container.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 Pichitron

try this :

尝试这个 :

org.glassfish.jersey.servlet.ServletContainer

org.glassfish.jersey.servlet.ServletContainer

on servlet-class

在 servlet 类上

回答by Paulo Oliveira

I had the same problem as you though I have followed a different guide: http://www.mkyong.com/webservices/jax-rs/jersey-hello-world-example/

我和你有同样的问题,虽然我遵循了不同的指南:http: //www.mkyong.com/webservices/jax-rs/jersey-hello-world-example/

The strange part is that, in this guide I have used, I should not have any problem with compatibility between versions (1.x against 2.x) because following the guide you use the jersey 1.8.x on pom.xmland in the web.xmlyou refer to a class (com.sun.jersey.spi.container.servlet.ServletContainer) as said before of 1.x version. So as I can infer this should be working.

奇怪的是,在我使用的本指南中,我不应该对版本之间的兼容性(1.x 对 2.x)有任何问题,因为按照指南,您将使用 1.8.x 上pom.xmlweb.xml您所指的球衣com.sun.jersey.spi.container.servlet.ServletContainer1.x 版本之前所说的类 ( )。所以我可以推断这应该有效。

My guessis because I'm using JDK 1.7 this class does not exist anymore.

我的猜测是因为我使用的是 JDK 1.7 这个类不再存在。



After, I tried to resolve with the answers before mine, did not helped, I have made changes on the pom.xmland on the web.xmlthe error changed to: java.lang.ClassNotFoundException: org.glassfish.jersey.servlet.ServletContainer

之后,我试图与答案解决矿难发生前,没有帮助,我已经在改变pom.xml,并在web.xml错误更改为:java.lang.ClassNotFoundException: org.glassfish.jersey.servlet.ServletContainer

Which supposedly should be exist!

这应该是存在的!

As result of this error, I found a "new" solution: http://marek.potociar.net/2013/06/13/jax-rs-2-0-and-jersey-2-0-released/

由于此错误,我找到了一个“新”解决方案:http: //marek.potociar.net/2013/06/13/jax-rs-2-0-and-jersey-2-0-released/

With Maven (archetypes), generate a jersey project, likes this:

用Maven(archetypes),生成一个球衣项目,像这样:

mvn archetype:generate -DarchetypeGroupId=org.glassfish.jersey.archetypes -DarchetypeArtifactId=jersey-quickstart-webapp -DarchetypeVersion=2.0

And it worked for me! :)

它对我有用!:)

回答by yogishaj

I also faced a similar issue. Resolved the problem by going through the step step tutorial from the below link.

我也遇到了类似的问题。通过以下链接中的步骤步骤教程解决了问题。

http://examples.javacodegeeks.com/enterprise-java/rest/jersey/jersey-hello-world-example/

http://examples.javacodegeeks.com/enterprise-java/rest/jersey/jersey-hello-world-example/

  • The main thing to notice is that the jersey libraries should be placed correctly in TOMCAT WEB-INF/lib folder. It is done automatically by the Eclipse settings mentioned in the above link. It will create a WAR file with the dependent JAR Files. Else, you will run into problems with ClassNotFound Exception.
  • 要注意的主要事情是球衣库应该正确放置在 TOMCAT WEB-INF/lib 文件夹中。它由上述链接中提到的 Eclipse 设置自动完成。它将使用依赖的 JAR 文件创建一个 WAR 文件。否则,您将遇到 ClassNotFound 异常的问题。

apache-tomcat-7.0.56-windows-x64\apache -tomcat-7.0.56\webapps\JerseyJSONExample\WEB-INF\lib

apache-tomcat-7.0.56-windows-x64\apache-tomcat-7.0.56\webapps\JerseyJSONExample\WEB-INF\lib

"11/23/2014 12:06 AM 130,458 jersey-client-1.9.jar

"11/23/2014 12:06 AM 130,458 jersey-client-1.9.jar

11/23/2014 12:06 AM 458,739 jersey-core-1.9.jar

11/23/2014 12:06 AM 458,739 jersey-core-1.9.jar

11/23/2014 12:06 AM 147,952 jersey-json-1.9.jar

11/23/2014 12:06 AM 147,952 jersey-json-1.9.jar

11/23/2014 12:06 AM 713,089 jersey-server-1.9.jar" 4 File(s) 1,450,238 bytes

11/23/2014 12:06 AM 713,089 jersey-server-1.9.jar" 4 文件 1,450,238 字节

  • The second tutorial explains about how to create a Webservice which produces and consumes JSON output.
  • 第二个教程解释了如何创建一个生成和使用 JSON 输出的 Web 服务。

http://examples.javacodegeeks.com/enterprise-java/rest/jersey/json-example-with-jersey-Hymanson/

http://examples.javacodegeeks.com/enterprise-java/rest/jersey/json-example-with-jersey-Hymanson/

Both the links gave a good picture on how things work and save a lot of time.

这两个链接都很好地说明了事情的运作方式并节省了大量时间。

回答by Sachin G N

We get this error because of build path issue. You should add "Server Runtime" libraries in Build Path.

由于构建路径问题,我们收到此错误。您应该在构建路径中添加“服务器运行时”库。

"java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer"

Please follow below steps to resolve class not found exception.

请按照以下步骤解决找不到类的异常。

Right click on project --> Build Path --> Java Build Path --> Add Library --> Server Runtime --> Apache Tomcat v7.0

右键单击 project --> Build Path --> Java Build Path --> Add Library --> Server Runtime --> Apache Tomcat v7.0

回答by shine

A simple workaround is , check whether you have dependencies or libs in deployment assembly of eclipse.probably if you are using tomcat , the server might not have identified the libs we are using . in that case specify it explicitly in deployment assembly.

一个简单的解决方法是,检查 eclipse 的部署程序集中是否有依赖项或库。可能如果您使用的是 tomcat,服务器可能没有识别我们正在使用的库。在这种情况下,在部署程序集中明确指定它。

回答by Andy1625

Coming back to the original problem - java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer

回到原来的问题 - java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer

As rightly said above, in JAX 2.x version, the ServletContainer class has been moved to the package - org.glassfish.jersey.servlet.ServletContainer. The related jar is jersey-container-servlet-core.jarwhich comes bundled within the jaxrs-ri-2.2.1.zip

如上所述,在 JAX 2.x 版本中,ServletContainer 类已移至包 - org.glassfish.jersey.servlet.ServletContainer。相关的 jar 是jersey-container-servlet-core.jar,它捆绑在jaxrs-ri-2.2.1.zip 中

JAX RS can be worked out without mvn by manually copying all jars contained within zip file jaxrs-ri-2.2.1.zip(i have used this version, would work with any 2.x version) to WEB-INF/lib folder. Copying libs to right folder makes them available at runtime.

通过手动将 zip 文件jaxrs-ri-2.2.1.zip 中包含的所有 jar (我使用过这个版本,可以与任何 2.x 版本一起使用)到 WEB-INF/lib 文件夹,可以在没有 mvn 的情况下计算出 JAX RS 。将库复制到正确的文件夹使它们在运行时可用。

This is required if you are using eclipse to build and deploy your project.

如果您使用 eclipse 来构建和部署您的项目,则这是必需的。