java.lang.NoSuchMethodError: org.glassfish.jersey.server.ApplicationHandler.<init>

16,724

Solution 1

Don't mix your Jersey versions

<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-grizzly2-http</artifactId>
    <version>2.9.1</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-server</artifactId>
    <version>2.25.1</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet-core</artifactId>
    <version>2.25.1</version>
</dependency>

Which one is different? Fix it.

And get rid of this

<dependency>
    <groupId>org.glassfish.grizzly</groupId>
    <artifactId>grizzly-http-all</artifactId>
    <version>2.3.30</version>
</dependency>

jersey-container-grizzly2-http already pulls in everything you need for Grizzly.

As an aside, the error says nothing about "GrizzlyHttpServerFactory doens't exist". It's saying that there is no such method (in this case constructor)

public ApplicationHandler(Application application, Binder binder) {}

That's how you read this

 java.lang.NoSuchMethodError: org.glassfish.jersey.server.ApplicationHandler.<init>(Ljavax/ws/rs/core/Application;Lorg/glassfish/hk2/utilities/Binder;)V

The reason you would get this error when mixing versions is because say some class in Jersey 2.25.1 is trying to call the constructor mentioned above. But in version 2.9.1 this constructor doesn't exist. Say it wasn't added til a later version. But when loading classes, the ApplicationHandler from 2.9.1 is loaded, instead of the one from 2.25.1. So now you have the wrong ApplicationHandler version loaded. Hence the error; there's no such constructor.

Solution 2

I was facing an issue similar to the one asked here with my Spring Boot Application. My application was unable to find LocalizationMessages class under org.glassfish.jersey.internal in place of org.glassfish.jersey.server.ApplicationHandler. with the same error java.lang.NoSuchMethodError .

java.lang.NoSuchMethodError: org.glassfish.jersey.internal.LocalizationMessages.WARNING_PROPERTIES()Ljava/lang/String

2021-09-20 15:27:47.927 INFO 11 --- [http-nio-9070-exec-7] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed; nested exception is java.lang.NoSuchMethodError: org.glassfish.jersey.internal.LocalizationMessages.WARNING_PROPERTIES()Ljava/lang/String;] with root cause

The irony is that the same set of dependencies worked fine on a java maven project. On getting the dependency tree mvn dependency:tree for java project I was able to get class org.glassfish.jersey.internal.LocalizationMessages but it was missing on the dependency tree for Spring Boot Project.

Following Issues observed:

  1. The Jar created by Spring Boot was not containing the dependency when I was using spring-boot-maven-plugin with version 2.4.3

      <plugin>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-maven-plugin</artifactId>
             <version>2.4.3</version>
         </plugin>
    
  2. When I was trying to create a shaded jar with the above version then I was getting error for all the Transformer implementation I was using. Error ex: Unable to parse configuration of mojo org.apache.maven.plugins:maven-shade-plugin:2.4.3:shade for parameter resource: Cannot find 'resource' in class org.apache.maven.plugins.shade.resource.ServicesResourceTransformer

  3. I changed the version to 1.5.8.RELEASE and then the shaded jar compilation went through.

  4. I replaced the default Spring Boot Application Plugin with the shaded jar Plugin.

Default Spring Boot Application Plugin

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot-version}</version>
                <configuration>
                    <mainClass>com.rohit.agrawal.Application</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

Shaded Jar Plugin I used to resolve error

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.1.1</version>
                <dependencies>
                    <dependency>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-maven-plugin</artifactId>
                        <version>${spring-boot-version}</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <keepDependenciesWithProvidedScope>true</keepDependenciesWithProvidedScope>
                    <createDependencyReducedPom>true</createDependencyReducedPom>
                    <filters>
                        <filter>
                            <artifact>*:*</artifact>
                            <excludes>
                                <exclude>META-INF/*.SF</exclude>
                                <exclude>META-INF/*.DSA</exclude>
                                <exclude>META-INF/*.RSA</exclude>
                            </excludes>
                        </filter>
                    </filters>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource>META-INF/spring.handlers</resource>
                                </transformer>
                                <transformer
                                        implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer">
                                    <resource>META-INF/spring.factories</resource>
                                </transformer>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource>META-INF/spring.schemas</resource>
                                </transformer>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.rohit.agrawal.Application</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot-version}</version>
            </plugin>
        </plugins>
    </build>

Please see that here spring-boot-version is 1.5.8.RELEASE

Now the Jar has the required class:

rohiagra-mac:faw-qa-api rohiagra$ jar -tvf target/faw-qa-api-1.0-SNAPSHOT.jar | grep "org.glassfish.jersey.internal.LocalizationMessages"
 42097 Tue Sep 21 18:01:54 IST 2021 BOOT-INF/classes/org/glassfish/jersey/internal/LocalizationMessages.class
   268 Tue Sep 21 18:01:56 IST 2021 BOOT-INF/classes/org/glassfish/jersey/internal/LocalizationMessages$1.class
  1272 Tue Sep 21 18:01:56 IST 2021 BOOT-INF/classes/org/glassfish/jersey/internal/LocalizationMessages$BundleSupplier.class
Share:
16,724

Related videos on Youtube

Stephen
Author by

Stephen

Growing in development with languages such as PHP, JavaScript, Java, and more. Currently working on own projects for developmental and experimental purposes.

Updated on June 04, 2022

Comments

  • Stephen
    Stephen almost 2 years

    I've been trying to debug this issue for a bit now, and searching SO and other websites I haven't been able to find a solution. I've checked all versions of the dependencies in my pom.xml and made sure they are compatible with grizzly2, and I've also verified that it is being imported. However, Java is still printing out this error upon run:

    Exception in thread "main" java.lang.NoSuchMethodError: org.glassfish.jersey.server.ApplicationHandler.<init>(Ljavax/ws/rs/core/Application;Lorg/glassfish/hk2/utilities/Binder;)V
    at org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainer.<init>(GrizzlyHttpContainer.java:334)
    at org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory.createHttpServer(GrizzlyHttpServerFactory.java:122)
    at com.kyrahosting.daemon.Main.startServer(Main.java:30)
    at com.kyrahosting.daemon.Main.main(Main.java:34)
    

    Here is my pom.xml: 4.0.0

    <groupId>com.kyrapanel.daemon</groupId>
    <packaging>jar</packaging>
    <name>KyraPanel Daemon</name>
    <artifactId>KyraDaemon</artifactId>
    <version>1.0-SNAPSHOT</version>
    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jersey</groupId>
                <artifactId>jersey-bom</artifactId>
                <version>${jersey.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-grizzly2-http</artifactId>
            <version>2.9.1</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.grizzly</groupId>
            <artifactId>grizzly-http-all</artifactId>
            <version>2.3.30</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
            <version>2.25.1</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <version>2.25.1</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.3.1</version>
        </dependency>
    </dependencies>
    
    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <!-- any other plugins -->
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <inherited>true</inherited>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>com.kyrahosting.daemon.Main</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
    
    <properties>
        <jersey.version>2.17</jersey.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    

    Any help will be appreciated!

  • Stephen
    Stephen almost 7 years
    Thank you for the very informative answer, this did the trick. Can't believe I didn't catch that version!
  • Ean V
    Ean V almost 5 years
    The most important part of this answer is the bit where it says: Don't mix your Jersey versions
  • Rohit Agrawal
    Rohit Agrawal over 2 years
    I have updated with Answer with the clarity and also quoted the output. The core issue or the problem is same. The solution is same for the question asked here and the one I had faced.
  • Rahul Bhiwagade
    Rahul Bhiwagade over 2 years
    it helped me created shaded jar for spring-boot application and I was able to see dependent class.