Zxing - java.lang.LinkageError:加载器约束违规

时间:2018-02-09 19:38:44

标签: java ejb zxing ear java-ee-7

我想在我的ejb bean中使用外部罐子(Zxing,Batik)并且根据this thread我将罐子添加到EarContent/lib并且一切似乎都没问题。我的ejb-bean看起来像:

@Stateless
public class ManageBarcodeBean implements ...{
    ...
    @Override
    public String createQrCode(String text, Dimension dim, ErrorCorrectionLevel errCorr) {
        ...
        try {
            ...
            if (Math.max(dim.getWidth(), dim.getHeight()) <= 1000) {
                //*
                QRCodeWriter qrCodeWriter = new QRCodeWriter();
                //**
                BitMatrix bm = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, dim.getWidth(), dim.getHeight(), hints);
                //67
                BufferedImage imgBuf = MatrixToImageWriter.toBufferedImage(bm);
                ...
            }
            ...
        } catch (Exception e) {
            ...
        } 
        return null;
    }
    ...
}

直到我添加行\\ 67一切正常,但添加此行会产生:

Caused by: java.lang.LinkageError:

loader constraint violation:

when resolving method
"com.google.zxing.client.j2se.MatrixToImageWriter.toBufferedImage(Lcom/google/zxing/common/BitMatrix;)Ljava/awt/image/BufferedImage;"

the class loader (instance of org/glassfish/javaee/full/deployment/EarClassLoader) 

of the current class, de/lsanet/lav/address/beans/ManageBarcodeBean,

and the class loader (instance of com/sun/enterprise/loader/CurrentBeforeParentClassLoader)

for the method's defining class, com/google/zxing/client/j2se/MatrixToImageWriter,

have different Class objects for the type com/google/zxing/common/BitMatrix

used in the signature at
de.lsanet.lav.address.beans.ManageBarcodeBean.createQrCode(ManageBarcodeBean.java:67)

我找到了this payara solution并添加了

<classloading-delegate>false</classloading-delegate>

解决了这个问题,但我不明白,为什么在core-3.3.2.jar*访问外部jar-1(Zxing:**)并且能够很好地访问外部jar-2 (Zxing:javase-3.3.2.jar)在67没有。

我不想影响容器类加载策略。

1 个答案:

答案 0 :(得分:0)

由于tsolakp假设正确,java.lang.LinkageError的原因是com.google.zxing.common.BitMatrix的两个版本。错误地,我将同一个jar放在${domain_dir}/libEARContent/lib中。