ClassNotFoundException:ClientBuilder Jersey 2.25.1 Tomcat 9.0

时间:2017-07-21 18:55:34

标签: java rest tomcat jersey classnotfoundexception

我几天来一直在努力解决这个问题,无法找到合适的解决方案。我已阅读过类似的问题,但所提供的解决方案都不够。

我的规格:Tomcat 9.0.0.M22,Jersey 2.25.1,Maven 4.0.0,IntelliJ 2017.1.4。

我的大脑绝对是什么事实,我可以在一个公共的静态void main(){}函数中毫无障碍地运行代码。我认为这意味着我的罐子被Maven正确包含了。但是,当我尝试通过ServletContextListener在Tomcat 9.0上运行它时,它会在ClientBuilder.newBuilder()。register(SseFeature.class).build()调用上冻结并抛出此错误。

Tomcat LocalHost日志:

21-Jul-2017 13:33:05.611 SEVERE [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core.StandardContext.listenerStart Exception sending context initialized event to listener instance of class [Web.Servlets.EventListener]
 java.lang.NoClassDefFoundError: org/glassfish/jersey/client/ClientBuilder
    at Web.Servlets.EventListener.contextInitialized(EventListener.java:19)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4633)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5095)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:740)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:716)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:703)
    at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1729)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
    at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
    at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:456)
    at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:405)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
    at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
    at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1468)
    at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76)
    at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309)
    at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1401)
    at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:346)
    at sun.rmi.transport.Transport$1.run(Transport.java:200)
    at sun.rmi.transport.Transport$1.run(Transport.java:197)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:683)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.ClassNotFoundException: org.glassfish.jersey.client.ClientBuilder
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1269)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1104)
    ... 46 more

我的pom.xml:

 <?xml version="1.0" encoding="UTF-8"?>
<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>groupId</groupId>
    <artifactId>EventStreams</artifactId>
    <version>1.0-SNAPSHOT</version>
   <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-sse</artifactId>
            <version>2.25.1</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-client</artifactId>
            <version>2.25.1</version>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>22.0</version>
        </dependency>
    </dependencies>
</project>

我的听众代码:

import org.glassfish.jersey.media.sse.SseFeature;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;

public class EventListener implements ServletContextListener {
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("I got in here");
        Client client1 = ClientBuilder.newBuilder().register(SseFeature.class).build();
        System.out.println("I never make it here");
    }
    public void contextDestroyed(ServletContextEvent sce) {}
}

解决方案1:我尝试将* jersey-client-2.25.1.jar,* jersey-media-sse-2.25.1.jar和* guava-22.0.jar放入我的WEB-INF / libs目录。
解决方案2:我使用的是Jersey 2.25.1 解决方案3:这个因为它们有不同的例外,所以它们并不相关 解决方案4:对pom.xml的更改没有任何区别 解决方案5:我尝试创建一个META-INF目录并添加所说的内容,但仍然没有 解决方案6:尝试添加指定的依赖项_不起作用,所以我立即将其删除。

我的项目目录:enter image description here

2 个答案:

答案 0 :(得分:1)

你必须使包装类型变得战争,在依赖项中包含你的jar,在终端中运行mvn build,导航到目标文件夹,你将在其中包含war文件。

答案 1 :(得分:0)

已解决

我没有在WEB-INF / libs目录中添加足够的库。我不得不进入项目结构 - &gt;文物 - &gt;在“可用元素”下 - &gt;右键单击“添加元素到WEB-INF / libs”。我手动添加了3个.jar文件,但显然我遗漏了一些东西。将我的整个外部库添加到libs目录后修复它。我删除后,我列出的6种解决方案都没有破坏我的应用程序。我如何到达这里是在阅读Tomcat Best Practices之后。我觉得这应该是由Maven以某种方式自动完成的,如果有人可以建议最好的做法随意这样做。

PS:我应该删除这个问题吗?