java.lang.LinkageError: loader constraint violation: when resolving method

22,754

Solution 1

I got this to work. My answer is just to emphasize what mvera is saying here.. E.g. Both pom.xml content for war and ejb modules shared the following dependency:

<dependencies>
...
    <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.14</version>         
    </dependency>
...
</dependencies>

Here is what i did ...

  1. Copy this dependency entry (and all shared dependencies) to the pom.xml in your ear module.

  2. Mark the shared dependencies as provided in the pom.xml files of your ejb and war modules such that resultant dependency entry in the pom.xml files now looks similar to below

    <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.14</version> 
            <scope>provided</scope>
    </dependency>
    
  3. Rebuild your project!

Solution 2

You war and ejb.jar have each their own classloader, so each classloader has its own definition of poi classes.

You should try to put poi jars and all common jars to your ejb.jar and war in ear/lib. Mark dependencies as "provided" in you pom.xml for ejb.jar and war.

This way both war and ejb.jar will inherit of classes defined by their parent classloader, the ear classloader.

You should never try to pass a class defined in a classloader to another classloader but use a class defined in a common classloader.

Share:
22,754
Bell Katapa
Author by

Bell Katapa

Updated on January 02, 2020

Comments

  • Bell Katapa
    Bell Katapa over 4 years

    I have the following:

    1. EJB in business tier

    package ejb.x.y;
    
    @Stateless
    public class FileFormatConvertor {  
        public byte[] fromExcelToCsv(Workbook workbook, Sheet sheet, String delimiter) throws Exception {
            ...
    }
    

    2. Bean in the web tier

    package web.x.y;
    
    @Named
    @SessionScoped
    public class FileUploadViewAction implements Serializable {
        @EJB
        private FileFormatConvertor fileFormatConvertor ;   
        // Other declarations
        if (fileType == Type.EXCEL) {
            bytesToUpload =   fileFormatConvertor.fromExcelToCsv(workbook, sheet, delimiter);
        }
        // Rest of code
    }
    

    3. POM Files

    Ear 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>
        <parent>
            <artifactId>xy-portal</artifactId>
            <groupId>com.xy</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <groupId>com.xy</groupId>
        <artifactId>ear-x-y</artifactId>
        <version>${project.version}</version>
        <packaging>ear</packaging>
        <name>ear-x-y</name>
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>
        <build>
            <finalName>ear-x-y</finalName>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>2.3.2</version>
                    <configuration>
                        <source>1.6</source>
                        <target>1.6</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-ear-plugin</artifactId>
                    <version>2.6</version>
                    <configuration>
                        <version>6</version>
                        <defaultLibBundleDir>lib</defaultLibBundleDir>
                        <modules>
                            <webModule>
                                <groupId>com.xy</groupId>
                                <artifactId>web-x-y</artifactId>
                                <contextRoot>/xy</contextRoot>
                                <bundleFileName>xy-portal-web.war</bundleFileName>
                            </webModule>
                            <ejbModule>
                                <groupId>com.xy</groupId>
                                <artifactId>ejb-x-y</artifactId>
                                <bundleFileName>xy-portal-ejb.jar</bundleFileName>
                            </ejbModule>
                        </modules>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.jboss.as.plugins</groupId>
                    <artifactId>jboss-as-maven-plugin</artifactId>
                    <configuration>
                        <skip>false</skip>  
                        <hostname>0.0.0.0</hostname>
                        <!--<hostname>xy</hostname>-->
                        <port>0000</port>
                        <filename>xy-portal-ear.ear</filename>
                    </configuration>
                </plugin>
            </plugins>
        </build>
        <dependencies>
            <dependency>
                <groupId>com.xy</groupId>
                <artifactId>xy-portal-ejb</artifactId>
                <version>${project.version}</version>
                <type>ejb</type>
            </dependency>   
            <dependency>
                <groupId>com.xy</groupId>
                <artifactId>xy-portal-web</artifactId>
                <version>${project.version}</version>
                <type>war</type>
            </dependency>
            <dependency>
                <groupId>org.jboss.resteasy</groupId>
                <artifactId>resteasy-jaxb-provider</artifactId>
                <version>2.3.1.GA</version>
            </dependency>
            <dependency>
                <groupId>org.jboss.resteasy</groupId>
                <artifactId>resteasy-jaxrs</artifactId>
                <version>2.3.1.GA</version>
            </dependency>
            <dependency>
                <groupId>${project.groupId}</groupId>
                <artifactId>xy-client</artifactId>
                <version>${project.version}</version>
            </dependency>
            <dependency>
                <groupId>${project.groupId}</groupId>
                <artifactId>xy-datamodel</artifactId>
                <version>${project.version}</version>
            </dependency>       
        </dependencies>    
    </project>
    

    Ejb 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>
        <parent>
            <artifactId>xy-portal</artifactId>
            <groupId>com.xy</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <groupId>com.xy</groupId>
        <artifactId>xy-portal-ejb</artifactId>
        <version>${project.version}</version>
        <packaging>ejb</packaging>
        <name>xy-portal-ejb</name>
        <properties>
            <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>
        <dependencies>          
            <dependency>
                <groupId>org.jboss.as</groupId>
                <artifactId>jboss-as-ejb3</artifactId>
                <version>7.1.2.Final</version>
                <scope>provided</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.hibernate</groupId>
                        <artifactId>hibernate-validator</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>      
            <dependency>
                <groupId>org.jboss.spec</groupId>
                <artifactId>jboss-javaee-6.0</artifactId>
                <version>3.0.3.Final</version>
                <type>pom</type>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.infinispan</groupId>
                <artifactId>infinispan-core</artifactId>
                <version>6.0.2.Final</version>
            </dependency>        
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi</artifactId>
                <version>3.14</version>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>3.14</version>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml-schemas</artifactId>
                <version>3.14</version>
            </dependency>
            <dependency>
                <groupId>dom4j</groupId>
                <artifactId>dom4j</artifactId>
                <version>1.6.1</version>
            </dependency>        
            <dependency>
               <groupId>org.testng</groupId>
               <artifactId>testng</artifactId>                           
               <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.jboss.arquillian.testng</groupId>
                <artifactId>arquillian-testng-container</artifactId>
                <scope>test</scope>
            </dependency>       
            <dependency>
                <groupId>org.jboss.shrinkwrap.resolver</groupId>
                <artifactId>shrinkwrap-resolver-api</artifactId>
                <scope>test</scope>
            </dependency>       
            <dependency>
                <groupId>org.jboss.shrinkwrap.resolver</groupId>
                <artifactId>shrinkwrap-resolver-api-maven</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.jboss.shrinkwrap.resolver</groupId>
                <artifactId>shrinkwrap-resolver-api-maven-archive</artifactId>
                <scope>test</scope>        
            </dependency>
            <dependency>
                <groupId>org.jboss.shrinkwrap.resolver</groupId>
                <artifactId>shrinkwrap-resolver-impl-maven-archive</artifactId>
                <scope>test</scope>        
            </dependency>
            <dependency>
                <groupId>org.eu.ingwar.tools</groupId>
                <artifactId>arquillian-suite-extension</artifactId>         
                <scope>test</scope>
            </dependency>       
            <dependency>
                <groupId>commons-io</groupId>
                <artifactId>commons-io</artifactId>
                <version>2.2</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>com.opencsv</groupId>
                <artifactId>opencsv</artifactId>
                <version>3.7</version>
                <scope>test</scope>
            </dependency>              
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>2.3.2</version>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-ejb-plugin</artifactId>
                    <version>2.3</version>
                    <configuration>
                        <ejbVersion>3.1</ejbVersion>
                        <archive>
                            <manifestEntries>
                              <Dependencies>org.infinispan export</Dependencies>
                            </manifestEntries> 
                        </archive>
                    </configuration>
                </plugin>       
            </plugins>
        </build>
    </project>
    

    Web 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>
        <parent>
            <artifactId>xy-portal</artifactId>
            <groupId>com.xy</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
    
        <artifactId>xy-portal-web</artifactId>
        <version>${project.version}</version>
        <packaging>war</packaging>
    
        <name>xy-portal-web</name>
    
        <properties>
            <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi</artifactId>
                <version>3.14</version>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>3.14</version>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml-schemas</artifactId>
                <version>3.14</version>
            </dependency>
            <dependency>
                <groupId>dom4j</groupId>
                <artifactId>dom4j</artifactId>
                <version>1.6.1</version>
            </dependency>
    
            <dependency>
                <groupId>org.primefaces.themes</groupId>
                <artifactId>all-themes</artifactId>
                <version>1.0.10</version>
            </dependency>
    
            <dependency>
                <groupId>org.primefaces</groupId>
                <artifactId>primefaces</artifactId>
                <version>5.2</version>
            </dependency>
    
            <dependency>
                <groupId>org.omnifaces</groupId>
                <artifactId>omnifaces</artifactId>
                <version>1.8.1</version>
            </dependency>
    
            <dependency>
                <groupId>org.primefaces.extensions</groupId>
                <artifactId>primefaces-extensions</artifactId>
                <version>3.2.0</version>
            </dependency>
    
            <dependency>
                <groupId>org.webjars</groupId>
                <artifactId>font-awesome</artifactId>
                <version>4.6.1</version>
            </dependency>       
    
            <dependency>
                <groupId>commons-fileupload</groupId>
                <artifactId>commons-fileupload</artifactId>
                <version>1.3</version>
            </dependency>
            <dependency>
                <groupId>commons-io</groupId>
                <artifactId>commons-io</artifactId>
                <version>2.2</version>
            </dependency>
            <dependency>
                <groupId>${project.groupId}</groupId>
                <artifactId>xy-client</artifactId>
                <version>${project.version}</version>
                <scope>provided</scope>
                <exclusions>                
                    <exclusion>
                        <groupId>org.hibernate</groupId>
                        <artifactId>hibernate-validator</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>${project.groupId}</groupId>
                <artifactId>xy-portal-ejb</artifactId>
                <version>${project.version}</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>${project.groupId}</groupId>
                <artifactId>xy-datamodel</artifactId>
                <version>${project.version}</version>
                <scope>provided</scope>
                <exclusions>
                    <exclusion>
                        <groupId>javax.validation</groupId>
                        <artifactId>validation-api</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.hibernate</groupId>
                        <artifactId>hibernate-validator</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.hibernate</groupId>
                        <artifactId>hibernate-jpamodelgen</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.jboss.resteasy</groupId>
                <artifactId>resteasy-jaxb-provider</artifactId>
                <version>2.3.1.GA</version>
            </dependency>
            <dependency>
                <groupId>org.jboss.resteasy</groupId>
                <artifactId>resteasy-jaxrs</artifactId>
                <version>2.3.1.GA</version>
            </dependency>
            <dependency>
                <groupId>org.testng</groupId>
                <artifactId>testng</artifactId>
                <version>6.8.1</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.jboss.resteasy</groupId>
                <artifactId>jaxrs-api</artifactId>
                <version>3.0.8.Final</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.17</version>
            </dependency>
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-validator</artifactId>
                <version>5.1.2.Final</version>
            </dependency>
            <dependency>
                <groupId>org.jboss.spec</groupId>
                <artifactId>jboss-javaee-web-6.0</artifactId>
                <version>3.0.3.Final</version>
                <type>pom</type>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.8.1</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>jboss</groupId>
                <artifactId>jbosssx</artifactId>
                <version>3.2.3</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>jboss</groupId>
                <artifactId>jboss-jaas</artifactId>
                <version>3.2.3</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>net.glxn</groupId>
                <artifactId>qrgen</artifactId>
                <version>1.4</version>
            </dependency>
            <dependency>
                <groupId>net.sf.barcode4j</groupId>
                <artifactId>barcode4j-light</artifactId>
                <version>2.0</version>
            </dependency>
            <dependency>
                <groupId>jexcelapi</groupId>
                <artifactId>jxl</artifactId>
                <version>2.6</version>
            </dependency>
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>2.3.2</version>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.1.1</version>
                    <configuration>
                        <failOnMissingWebXml>false</failOnMissingWebXml>
                    </configuration>
                </plugin>           
            </plugins>
        </build>
    </project>
    

    4. Exception

    Caused by: javax.faces.el.EvaluationException: java.lang.LinkageError: loader constraint violation: when resolving method "ejb.x.y.FileFormatConvertor.fromExcelToCsv(Lorg/apache/poi/ss/usermodel/Workbook;Lorg/apache/poi/ss/usermodel/Sheet;Ljava/lang/String;)[B" the class loader (instance of org/jboss/modules/ModuleClassLoader) of the current class, web.x.y.FileUploadViewAction, and the class loader (instance of org/jboss/modules/ModuleClassLoader) for resolved class, ejb.x.y.FileFormatConvertor, have different Class objects for the type Lorg/apache/poi/ss/usermodel/Workbook;Lorg/apache/poi/ss/usermodel/Sheet;Ljava/lang/String;)[B used in the signature
        at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
        at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102) [jsf-impl-2.1.7-jbossorg-2.jar:]
        ... 36 more
    

    What am I doing wrong to be getting this exception? So far I have done the following: 1. Check for cyclic class path dependencies 2. Move the ejb to web tier as managed bean (solves the problem but the bean is accessed elsewhere by other clients, making the business tier a more natural home) 3. Searched this site for similar problem/solution trails but have found none

    Please help...

  • Steve C
    Steve C almost 8 years
    There is no such thing as an EAR classloader. All the jars in an EAR file's lib directory are deemed to be in their own module, which is made visible to the modules in the EAR such as EJB jars and WAR files. The EAR/lib module effectively gets its own class loader, but it is certainly not the "parent". Otherwise your answer is fine.