javaqr-codezxing

java.lang.NoSuchMethodError: com.google.zxing.client.j2se.MatrixToImageWriter.writeToPath()


I have written the Java program to generate a QR Code By using the com.google.zxing.But with some java versions return java.lang.NoSuchMethodError:com.google.zxing.client.j2se.MatrixToImageWriter.writeToPath() error.

I have written the following Java program to generate a QR Code By using the following dependency. It works with the following java versions

OpenJDK version "1.8.0_342-internal"
OpenJDK Runtime Environment (build 1.8.0_342-internal-b07)
OpenJDK 64-Bit Server VM (build 25.342-b07, mixed mode)

java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)

But unfortunately, With the following version
Java version "1.8.0_191" Java(TM) SE Runtime Environment (build 1.8.0_191-b12) Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)

the following error 
java.lang.NoSuchMethodError: com.google.zxing.client.j2se.MatrixToImageWriter.writeToPath(Lcom/google/zxing/common/BitMatrix;Ljava/lang/String;Ljava/nio/file/Path;)

01). Dependencies Used

`<dependencies>
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>3.3.0</version>
        </dependency>
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>javase</artifactId>
            <version>3.3.0</version>
        </dependency>
    </dependencies>`

02).Programme

package com.dataz.core;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

public class QrCodeUtil {

    public static boolean generateImageGenaric(String text, String filePath, Integer width, Integer height)
        throws WriterException, IOException {
    QRCodeWriter qrCodeWriter = new QRCodeWriter();
    BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
    Path path = FileSystems.getDefault().getPath(filePath);
    MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
    // MatrixToImageWriter.toBufferedImage(bitMatrix);
    return true;
    }

    public static void main(String[] args) {
    try {
        String path = "/home/data/qr/qrfile.png";
        String value = "qrbatta";
        if (args != null && args.length > 0 && args[0] != null) {
        path = args[0];
        }
        if (args != null && args.length > 1 && args[1] != null) {
        value = args[1];
        }

        boolean isgenerated = generateImageGenaric(value, path, 400, 400);
        if (isgenerated) {
        System.out.println("QR Code Generate In Here --->" + path);
        } else {
        System.out.println("Error Occured While Generating");
        }

    } catch (WriterException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }
}
 org.glassfish.jersey.server.ContainerException: java.lang.NoSuchMethodError: com.google.zxing.client.j2se.MatrixToImageWriter.writeToPath(Lcom&#47;google&#47;zxing&#47;common&#47;BitMatrix;Ljava&#47;lang&#47;String;Ljava&#47;nio&#47;file&#47;Path;)V
    org.glassfish.jersey.servlet.internal.ResponseWriter.rethrow(ResponseWriter.java:256)
    org.glassfish.jersey.servlet.internal.ResponseWriter.failure(ResponseWriter.java:238)
    org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:486)
    org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:317)
    org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
    org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
    org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    org.glassfish.jersey.internal.Errors.process(Errors.java:267)
    org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
    org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:292)
    org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1139)
    org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:454)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:386)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:334)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
</pre>
    <p><b>Root Cause</b></p>
    <pre>java.lang.NoSuchMethodError: com.google.zxing.client.j2se.MatrixToImageWriter.writeToPath(Lcom&#47;google&#47;zxing&#47;common&#47;BitMatrix;Ljava&#47;lang&#47;String;Ljava&#47;nio&#47;file&#47;Path;)V
    com.dataz.core.QrCodeUtil.generateImageGenaric(QrCodeUtil.java:20)
    com.dataz.core.customerinfo_enterprice_contacts.CustomerinfoEnterpriceContactsDataManager.generateQRCode(CustomerinfoEnterpriceContactsDataManager.java:354)
    com.dataz.core.customerinfo_enterprice_contacts.CustomerinfoEnterpriceContactsDataManager.sendEmailToCustomer(CustomerinfoEnterpriceContactsDataManager.java:303)
    com.dataz.core.customerinfo_enterprice_contacts.CustomerinfoEnterpriceContactsDataManager.addCustomerinfoEnterpriceContacts(CustomerinfoEnterpriceContactsDataManager.java:278)
    com.dataz.core.api.vi.service.impl.CustomerinfoEnterpriceContactsServiceImpl.addCustomerinfoEnterpriceContacts(CustomerinfoEnterpriceContactsServiceImpl.java:181)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:498)
    org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
    org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:144)
    org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:161)
    org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:159)
    org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99)
    org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
    org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
    org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
    org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:309)
    org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
    org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
    org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    org.glassfish.jersey.internal.Errors.process(Errors.java:267)
    org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
    org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:292)
    org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1139)
    org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:454)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:386)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:334)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

Solution

  • This is due to different versions of the zxing library being in the classpath. As the poster has indicated in comments, there was another library depending on an older version of the zxing library.

    In general, when there is a NoSuchMethodError which occurs inconsistently, it is advisable to check for conflicts of the same class, as we won't be sure which version of the class will be loaded.