Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.NoClassDefFoundError: org/glassfish/jersey/internal/inject/Binder when started Tomcat Server

I am building REST API in Java with Jersey and Maven. I used Tomcat 9 as my server. Everything works fine until I tried to install RestAssured, Hamcrest, and JUnit today. Suddenly all of my endpoints threw out 500 Internal server errors. The root cause of the 500 error is java.lang.NoClassDefFoundError: org/glassfish/jersey/internal/inject/Binder.

What I have done this 2 hours:

  • I have tried to find this class: org/glassfish/jersey/internal/inject/Binder on google, but no avail.
  • I have tried to uninstall RestAssured, Hamcrest, and JUnit but it didn't help.

This problem makes me frustrated. Any idea why this errors happened? Thanks in advance!

Here is the excerpt of the server log:

SEVERE: Allocate exception for servlet Jersey Web Application java.lang.ClassNotFoundException: org.glassfish.jersey.internal.inject.Binder at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1275) at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1109) at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:178) at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:370) at javax.servlet.GenericServlet.init(GenericServlet.java:158) at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1183) at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1099) at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:779) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:133) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:475) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:341) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:498) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:796) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1368) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Unknown Source)

Below is my pom.xml file: (with RestAssured, Hamcrest, and JUnit)

<dependencies>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
        <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
        <!-- artifactId>jersey-container-servlet</artifactId -->
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-moxy</artifactId>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.0</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-all</artifactId>
        <version>1.3</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.jayway.restassured</groupId>
        <artifactId>rest-assured</artifactId>
        <version>2.9.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>

Below is my web.xml file:

<web-app version="2.5" 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_2_5.xsd">
<servlet>
    <servlet-name>Jersey Web Application</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.accelbyte.vincent.emailist.resources</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Jersey Web Application</servlet-name>
    <url-pattern>/webapi/*</url-pattern>
</servlet-mapping>

Below is my project structure:

Email list project structure

like image 451
Vincent acent Avatar asked Apr 19 '17 04:04

Vincent acent


3 Answers

Very likely there is a version mismatch between core Jersey and one of the Jersey extension libraries that imports that Binder class. Make sure all Jersey-related modules have explicit versions (and they match each other). Or even better, fix your versions by importing the jersey-bom. E.g.:

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

After which your main <dependencies> section may omit the versions.

like image 82
andrus_a Avatar answered Nov 20 '22 17:11

andrus_a


I have faced the same issue with using different versions. Make sure your Jersey-bean-validation and Jersey-container-servlet versions are the same.

<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet</artifactId>
    <version>2.22</version>
</dependency>
<!-- bean validation -->
<dependency>
    <groupId>org.glassfish.jersey.ext</groupId>
    <artifactId>jersey-bean-validation</artifactId>
    <version>2.22</version>
</dependency>
like image 27
Srikanth Bathi Avatar answered Nov 20 '22 16:11

Srikanth Bathi


Same problem with you. keeping the jersey-bean-validation and jersey-container-servlet the same version works well.

like image 1
wangsir Avatar answered Nov 20 '22 16:11

wangsir