0

I have been struggling with this for a few days now and unable to find a proper solution. I have read through similar questions but none of the solutions provided were sufficient.

My specs: Tomcat 9.0.0.M22, Jersey 2.25.1, Maven 4.0.0, IntelliJ 2017.1.4.

What is absolutely racking my brain is the fact that I can run the code absolutely fine without a hitch in a public static void main(){} function. I presume this means that I my jar's are being included properly by Maven. But, when I try to run it on Tomcat 9.0 through a ServletContextListener it freezes on the ClientBuilder.newBuilder().register(SseFeature.class).build() call and throws this error.

Tomcat LocalHost Log:

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

My 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>

My Listener code:

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) {}
}

Solution 1: I tried putting *jersey-client-2.25.1.jar, *jersey-media-sse-2.25.1.jar and *guava-22.0.jar in my WEB-INF/libs directory.
Solution 2: I am using Jersey 2.25.1.
Solution 3: This one is not relevant really since they had a different exception.
Solution 4: Changes to pom.xml made no difference.
Solution 5: I tried creating a META-INF directory and adding what was told, still nothing.
Solution 6: Tried adding the dependency specified_ did not work so I promptly removed it.

My Project directory: enter image description here

code_disciple
  • 182
  • 2
  • 17
  • what is packaging type of your application? I assume you are creating a war file but this is not obvious in your pom. So, how are you packaging it? – Imran Jul 21 '17 at 19:26
  • I am creating an exploded war file through the Project Structure -> Artifacts Window. Are you saying that this should be specified in the pom.xml, not manually set once I start the project? – code_disciple Jul 21 '17 at 19:40

2 Answers2

1

You have to make the packaging type war, include your jars in dependencies, run mvn build in terminal, navigate to target folder where you will have a war file everything included in.

Imran
  • 1,732
  • 3
  • 21
  • 46
0

Solved :

I hadn't added enough of the libraries to my WEB-INF/libs directory. I had to go into Project Structure -> Artifacts -> Under "Available Elements" -> Right-Click Add Elements to WEB-INF/libs. I added 3 .jar files manually but apparently I was missing something. After I added my entire external library to the libs directory fixed it. None of the 6 solutions I listed broke my app after I removed them. How I arrived at this was after reading Tomcat Best Practices. I feel like this should have been done automatically somehow by Maven, if anyone can suggest a best practice feel free to do so.

PS: Should I delete this question?

Community
  • 1
  • 1
code_disciple
  • 182
  • 2
  • 17
  • why are you using maven when you are adding them manually? You should add them inti maven dependencies and it should build the war for you. You are not using maven properly. – Imran Jul 21 '17 at 19:46
  • Well maven will put them into my "External Libraries" for me. This is why I was able to run the code in a java class. It did that automatically for me through auto-import after I added the dependencies to my pom.xml, but for some reason I didn't set up the project correctly to have them added to my war as well. That's where I had to do it manually ("Available Elements" -> Ctrl-A -> Right-Click -> Add to WEB-INF/libs). It wasn't much trouble, but yes I do feel I could be using maven more efficiently. – code_disciple Jul 21 '17 at 19:57