javamavenspring-mvclinkageerror

java.lang.LinkageError: ClassCastException: in spring MVC project


I am working on spring mvc and jersey rest client project 1 and project 2 as a spring core .Throws exception while execute code in tomcat server.

Exception : Caught throwable ClassCastException: attempting to castjar:file:/project1/web/webroot/WEB-INF/lib/jersey-core-1.13.jar!/javax/ws/rs/ext/RuntimeDelegate.classtojar:file:/project2/lib/jersey-core-1.13.jar!/javax/ws/rs/ext/RuntimeDelegate.class java.lang.LinkageError: ClassCastException: attempting to castjar:file:/project1/web/webroot/WEB-INF/lib/jersey-core-1.13.jar! /javax/ws/rs/ext/RuntimeDelegate.classtojar:file:/project2/lib/jersey-core-1.13.jar!/javax/ws/rs/ext/RuntimeDelegate.class

My project 2 maven dependencies:

    <properties>
            <jersey.version>1.13</jersey.version>
        </properties>

        <dependencies>
            <dependency>
                <groupId>com.sun.jersey</groupId>
                <artifactId>jersey-core</artifactId>
                <version>${jersey.version}</version>
            </dependency>
            <dependency>
                <groupId>com.sun.jersey</groupId>
                <artifactId>jersey-client</artifactId>
                <version>${jersey.version}</version>
                <exclusions>
                    <exclusion>
                        <groupId>com.sun.jersey</groupId>
                        <artifactId>jersey-core</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.json</groupId>
                <artifactId>json</artifactId>
                <version>20090211</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.module</groupId>
                <artifactId>jackson-module-jaxb-annotations</artifactId>
                <version>2.4.0</version>
            </dependency>  

project 1 maven dependencies:

    <properties>
        <jersey.version>1.13</jersey.version>
    </properties>

    <dependencies>
            <dependency>
          <groupId>com.sun.jersey</groupId>
          <artifactId>jersey-core</artifactId>
          <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-client</artifactId>
          <version>${jersey.version}</version>
          <exclusions>
                <exclusion>
                    <groupId>com.sun.jersey</groupId>
                    <artifactId>jersey-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>   
        <dependency>
          <groupId>com.sun.jersey</groupId>
          <artifactId>jersey-json</artifactId>
          <version>${jersey.version}</version>
        </dependency>
        <dependency>
          <groupId>com.sun.jersey</groupId>
          <artifactId>jersey-server</artifactId>
          <version>${jersey.version}</version>
        </dependency>
        <dependency>
          <groupId>com.sun.jersey</groupId>
          <artifactId>jersey-servlet</artifactId>
          <version>${jersey.version}</version>
        </dependency>
        <dependency>
          <groupId>com.sun.jersey.contribs</groupId>
          <artifactId>jersey-spring</artifactId>
          <version>${jersey.version}</version>
        </dependency>
        <dependency>
           <groupId>com.sun.jersey.jersey-test-framework</groupId>
           <artifactId>jersey-test-framework-core</artifactId>
            <version>${jersey.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>jsr311-api</artifactId>
            <version>1.1.1</version>
        </dependency>
<dependency>
    <groupId>org.springframework.security.oauth</groupId>
    <artifactId>spring-security-oauth2</artifactId>
    <version>2.0.6.RELEASE</version>
     <exclusions>
                <exclusion>
                    <groupId>com.sun.jersey</groupId>
                    <artifactId>jersey-client</artifactId>
                </exclusion>
            </exclusions>
</dependency>
<dependency>
    <groupId>net.sourceforge.javacsv</groupId>
    <artifactId>javacsv</artifactId>
    <version>2.0</version>
</dependency>

Solution

  • You are having jersy present in the both projects. This will make different class loaders to load jersy class and thus you will get class cast exception. Make sure jersy is loaded only once. you can do this by putting your jersy jar in server library and using maven scope as provided.