java.lang.LinkageError:加载器约束冲突:解析方法时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37207666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 02:17:56  来源:igfitidea点击:

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

javamavenjakarta-eejboss

提问by Bell Katapa

I have the following:

我有以下几点:

1. EJB in business tier

1.业务层的EJB

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

2.web层中的Bean

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

3. POM 文件

Ear pom.xml

耳朵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

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

网页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

4. 例外

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

得到这个例外我做错了什么?到目前为止,我已经完成了以下工作: 1. 检查循环类路径依赖项 2. 将 ejb 作为托管 bean 移动到 web 层(解决了该问题,但其他客户端可以访问该 bean,使业务层成为更自然的家) 3. 在本站搜索类似的问题/解决方案,但没有找到

Please help...

请帮忙...

回答by mvera

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

war 和 ejb.jar 都有自己的类加载器,所以每个类加载器都有自己的 poi 类定义。

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.

你应该尝试把 poi jars 和所有常见的 jars 放到你的 ejb.jar 和 war 中。在 ejb.jar 和 war 的 pom.xml 中将依赖项标记为“提供”。

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

这样,war 和 ejb.jar 都将继承由它们的父类加载器(ear 类加载器)定义的类。

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

永远不要尝试将类加载器中定义的类传递给另一个类加载器,而应该使用公共类加载器中定义的类。

回答by Bell Katapa

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:

我得到了这个工作。我的回答只是强调 mvera 在这里所说的内容。例如,war 和 ejb 模块的 pom.xml 内容共享以下依赖项:

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

  1. 将此依赖项(以及所有共享依赖项)复制到您的 ear 模块中的 pom.xml。

  2. 将共享依赖项标记为 ejb 和 war 模块的 pom.xml 文件中提供的,这样 pom.xml 文件中的结果依赖项条目现在看起来类似于以下内容

    <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.14</version> 
            <scope>provided</scope>
    </dependency>
    
  3. 重建你的项目!