Java Tomcat Jersey Eclipse ClassNotFound org.glassfish.jersey.servlet.ServletContainer

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

Tomcat Jersey Eclipse ClassNotFound org.glassfish.jersey.servlet.ServletContainer

javaeclipsemavenservletsjersey

提问by jeremyvillalobos

I am running the hello worldexample.

我正在运行hello world示例。

However, I am using version 2.7.

但是,我使用的是 2.7 版。

on maven pom.xml I have

在 maven 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mydomain.restful</groupId>
<artifactId>AdvertServer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Advert Server</name>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey</groupId>
            <artifactId>jersey-bom</artifactId>
            <version>2.7</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.2.4</version>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>6.0</version>
    </dependency>
    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>javax.ws.rs-api</artifactId>
        <version>2.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-client</artifactId>
        <version>2.7</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
        <version>2.7</version>
        <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
        <!-- artifactId>jersey-container-servlet</artifactId -->
    </dependency>
</dependencies>

<build>
    <sourceDirectory>src</sourceDirectory>
    <resources>
        <resource>
            <directory>src</directory>
            <excludes>
                <exclude>**/*.java</exclude>
            </excludes>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.0</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
</build>
</project>

I tried running the servlet from command line and from withing the Server plugin on Eclipse. I get the same error:

我尝试从命令行和 Eclipse 上的服务器插件运行 servlet。我犯了同样的错误:

java.lang.ClassNotFoundException: org.glassfish.jersey.servlet.ServletContainer
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1678)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1523)
    at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:525)
    at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:507)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:126)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1099)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1043)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4957)
    at org.apache.catalina.core.StandardContext.call(StandardContext.java:5284)
    at org.apache.catalina.core.StandardContext.call(StandardContext.java:5279)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    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:722)

The weird thing is that I can see the class among the .jar files. The class is there.

奇怪的是我可以在 .jar 文件中看到这个类。班级在那里。

I have even tried adding the jar skipping maven, but Tomcat won't see the class.

我什至尝试添加跳过 maven 的 jar,但 Tomcat 看不到该类。

采纳答案by jeremyvillalobos

From

Eclipse + tomcat - ClassNotFound exception on deploy

Eclipse + tomcat - 部署时 ClassNotFound 异常

If you have installed both the Maven Eclipse Plugin and the Maven WTP Plugin this is automatic. If you don't have these installed, go ahead and install them and then once Eclipse restarts right-click on the project and do a Maven > Update project.... This will internally change the project configuration so the Maven dependencies are copied to the /WEB-INF/lib folder on your deployment target.

If you don't want to use any of these plug-ins, then you have to go to the Project properties > Deployment Assembly configuration and add your dependencies manually, but again, this is done automatically by these plug-ins.

如果您已经安装了 Maven Eclipse 插件和 Maven WTP 插件,这是自动的。如果您没有安装这些,请继续安装它们,然后在 Eclipse 重新启动后右键单击项目并执行 Maven > 更新项目...。这将在内部更改项目配置,以便将 Maven 依赖项复制到部署目标上的 /WEB-INF/lib 文件夹。

如果您不想使用这些插件中的任何一个,那么您必须转到“项目属性”>“部署程序集”配置并手动添加您的依赖项,但同样,这是由这些插件自动完成的。

回答by raspayu

Just a hint...

只是一个提示...

At the end, what you are creating are good old war files... which should have your libraries under WEB-INF/lib. Have you checked if it is happening? I mean, you can create your war (Export/WAR file right clicking on the project in the project explorer), and then, through your zip application, go to that WEB-INF/lib folder in the war file, and checking that your jar files are there.

最后,您正在创建的是很好的旧战争文件……它们应该在 WEB-INF/lib 下有您的库。你有没有检查它是否发生?我的意思是,您可以创建您的 war(在项目资源管理器中右键单击该项目,导出/WAR 文件),然后通过您的 zip 应用程序,转到 war 文件中的那个 WEB-INF/lib 文件夹,并检查您的jar 文件在那里。

If they are not there, Eclipse is not exporting your libraries into the war file correctly. Have you added them to your build path(java build path / Libraries and java build path / Order and export)?

如果它们不存在,则 Eclipse 不会将您的库正确导出到 war 文件中。您是否将它们添加到您的构建路径(java 构建路径 / 库和 java 构建路径 / 订购和导出)?

回答by ivanceras

Add

添加

<packaging>war</packaging>

in your pom.xml, I experienced this problem when I momentarily change the packing to jar when by using the library via

在您的 pom.xml 中,当我通过使用库时暂时将包装更改为 jar 时,我遇到了这个问题

 mvn clean install

which compile it to jar and can be referenced by your other projects, which is not possible to do when your packaging is war. When you forgot to change the packing back to war I see that exact error stacktrace comes up

哪个编译成jar,可以被你的其他项目引用,这在你打包war的时候是做不到的。当您忘记将包装改回战争时,我看到出现了确切的错误堆栈跟踪