Home > OS >  ZXing java.lang.NoClassDefFoundError: com/google/zxing/WriterException Error
ZXing java.lang.NoClassDefFoundError: com/google/zxing/WriterException Error

Time:07-23

Project Context

Currently trying to generate QR codes to implement some information within it, that includes data (such as a student ID number, first name, last name) and MAC address of local computer (testing on my desktop computer for now, but will be applied to individual raspberry Pis).

Error in Question

enter image description here

The Java Code

Ignore the data link input, this is for test purposes only.

    public static void main(String[] args) throws Exception
{        
    MACFinder GM = new MACFinder();
    GM.getMac();
    
    System.out.println();
    
    try
    {
        GM.generateQRCodeImage();
    }
    catch (WriterException e)
    {
        System.out.println("Couldn't generate QR code, WriterException...");
    }
    catch (IOException e)
    {
        System.out.println("Couldn't generate QR code, IOException...");
    }
    catch (NotFoundException e)
    {
        System.out.println("This library couldn't be found... (ZXing)");
    }
}

    public void generateQRCodeImage() throws WriterException, IOException, NotFoundException
{
    String data = "https://www.youtube.com/watch?v=qzYpgbP8RHA";
    String filepath = "C:\\Users\\JmJ23\\Documents\\Code\\Internship Code";
    
    QRCodeWriter qrCodeWriter = new QRCodeWriter();
    BitMatrix bitMatrix = qrCodeWriter.encode(data, BarcodeFormat.QR_CODE, 250, 250);

    Path path = FileSystems.getDefault().getPath(filepath);
    MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
}

POM.xml 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>

I understand that a NoClassDefFoundError is when a library/class is called but the class in question is not found at runtime... but why would this be the case if I have installed it into my Maven project? As a note I cleaned the project multiple times but with no progress. Any advice?

CodePudding user response:

Your build looks fine. You didn't say how you build and run this, but I expect you just tried to build a .jar and run that .jar, without building all of its dependencies into it. You need something like the Maven assembly plugin.

  • Related