eclipse 严重:Servlet /JAXRS-HelloWorld 抛出 load() 异常 java.lang.ClassNotFoundException:com.sun.jersey.spi.container.servlet.ServletC
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33608943/
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
SEVERE: Servlet /JAXRS-HelloWorld threw load() exception java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer
提问by DJ-
I read through plenty of posts related to this issue but couldn't get any help .
我阅读了大量与此问题相关的帖子,但没有得到任何帮助。
ERROR Appreciate if someone can help me out.
错误感谢有人可以帮助我。
This is how my POM and web looks like . I have tried copying the required war file to C:\appache tomcat 7 but no luck. Even deploying it within the eclipse also gives me the same error.
这就是我的 POM 和 web 的样子。我曾尝试将所需的War文件复制到 C:\appache tomcat 7,但没有成功。即使在 eclipse 中部署它也会给我同样的错误。
WEb enter image description here
WEB 在此输入图片说明
POM.xML:-
POM.xml:-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tutorialacademy.rest</groupId>
<artifactId>helloworld</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>helloworld Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories>
<repository>
<id>maven2-repository.java.net</id>
<name>Java.net Repository for Maven</name>
<url>http://download.java.net/maven/2/</url>
<layout>default</layout>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-apache-client</artifactId>
<version>1.19</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
回答by cassiomolin
By the exception, it looks like the com.sun.jersey.spi.container.servlet.ServletContainer
class is not available in your classpath.
作为例外,看起来com.sun.jersey.spi.container.servlet.ServletContainer
该类在您的类路径中不可用。
Note that Jersey 1.xand Jersey 2.xuse different package names:
请注意Jersey 1.x和Jersey 2.x使用不同的包名称:
- Jersey 1.x:
com.sun.jersey
- Jersey 2.x:
org.glassfish.jersey
- 球衣 1.x:
com.sun.jersey
- 泽西岛 2.x:
org.glassfish.jersey
So, I understand you are using Jersey 1.x.
所以,我知道您正在使用Jersey 1.x。
Jersey 1.x depencency
Jersey 1.x 依赖
To use Jersey 1.x, you need the following dependecy in your pom.xml
:
要使用Jersey 1.x,您需要在您的以下依赖项pom.xml
:
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.19</version>
</dependency>
To read more about Jersey 1.x dependencies, have a look at the documentation.
要阅读有关 Jersey 1.x 依赖项的更多信息,请查看文档。
You can check the latest version of the jersey-server
artifactId
in the Maven Repository.
您可以jersey-server
artifactId
在Maven Repository 中查看最新版本的。
Jersey 2.x depencency
Jersey 2.x 依赖
If you want to use Jersey 2.x, you have to add the following dependency to your pom.xml
:
如果要使用Jersey 2.x,则必须将以下依赖项添加到您的pom.xml
:
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<!-- if your container implements Servlet API older than 3.0,
use "jersey-container-servlet-core" -->
<artifactId>jersey-container-servlet</artifactId>
<version>2.22.1</version>
</dependency>
You can check the latest version of the jersey-container-servlet
artifactId
in the Maven Repository.
您可以jersey-container-servlet
artifactId
在Maven Repository 中查看最新版本的。
Read more about Jersey 2.x dependencies in the documentation.
在文档中阅读有关 Jersey 2.x 依赖项的更多信息。
UPDATE
更新
If you can, I do recommend using Jersey 2.xrather than Jersey 1.x.
如果可以,我建议您使用Jersey 2.x而不是Jersey 1.x。
To do it, use the following pom.xml
:
为此,请使用以下命令pom.xml
:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tutorialacademy.rest</groupId>
<artifactId>helloworld</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Hello World</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories>
<repository>
<id>maven2-repository.java.net</id>
<name>Java.net Repository for Maven</name>
<url>http://download.java.net/maven/2/</url>
<layout>default</layout>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<!-- if your container implements Servlet API older than 3.0,
use "jersey-container-servlet-core" -->
<artifactId>jersey-container-servlet</artifactId>
<version>2.22.1</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
And the following web.xml
:
以及以下内容web.xml
:
<web-app 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_3_0.xsd"
version="3.0">
</web-app>
回答by santoshM
If you are trying to make a Web application to provide web service using Jersey library in Tomcat and Eclipse and still getting problem after adding jersey-server.jar in the classpath. You can follow these steps to add dependency on deployment assembly.
如果您尝试使用 Tomcat 和 Eclipse 中的 Jersey 库制作 Web 应用程序以提供 Web 服务,并且在类路径中添加 jersey-server.jar 后仍然遇到问题。您可以按照以下步骤添加对部署程序集的依赖。
Project -> properties -> development assembly -> add -> java build path entries -> maven dependency
项目->属性->开发程序集->添加->java构建路径条目->maven依赖
This should work.
这应该有效。