简体   繁体   中英

Zxing - java.lang.LinkageError: loader constraint violation

I want to use external jars (Zxing, Batik) in my ejb bean and according to this thread I added the jars to EarContent/lib and everything seemed to be ok. My ejb-bean looks like:

@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;
    }
    ...
}

until I add line \\\\ 67 everything works fine, but adding this line yields:

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)

I found this payara solution and adding

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

solves the problem, but I don't understand, why accessing external jar-1 (Zxing: core-3.3.2.jar ) at * and ** works perfectly well and accessing external jar-2 (Zxing: javase-3.3.2.jar ) at 67 does not.

I would prefer not to influence the containers class loading strategy.

As tsolakp supposed right, the reason for the java.lang.LinkageError were two versions of com.google.zxing.common.BitMatrix . By mistake, I placed the same jar in ${domain_dir}/lib and in EARContent/lib .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM