Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.NoSuchFieldError: Factory error when using Apache POI XSSFWorkbook in Payara project

Tags:

apache-poi

I am encountering a runtime error in my Payara project that involves the usage of Apache POI's XSSFWorkbook class. The error message is as follows:

java.lang.NoSuchFieldError: Factory
at org.apache.poi.xssf.usermodel.XSSFWorkbook.onWorkbookCreate(XSSFWorkbook.java:506)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:233)
// ... (more stack trace)

Context:

I'm working on a Payara project using Java EE technologies. The error occurs when trying to create an instance of XSSFWorkbook within the method createTemplateForItemWithFeeUpload in the DataUploadBean class. I've included the relevant dependencies in my pom.xml, including Apache POI's poi-ooxml version 5.2.5.

Relevant Dependencies (from pom.xml):

<!-- Apache POI -->
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>5.2.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>5.2.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-excelant</artifactId>
        <version>5.2.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-scratchpad</artifactId>
        <version>5.2.5</version>
    </dependency>
    <dependency>
        <groupId>fr.opensagres.xdocreport</groupId>
        <artifactId>org.apache.poi.xwpf.converter.xhtml</artifactId>
        <version>1.0.6</version>
    </dependency>

Relevant method causing the issue

public void createTemplateForItemWithFeeUpload() {
    try {
        // The following line triggers the NoSuchFieldError
        XSSFWorkbook workbook = new XSSFWorkbook();  
        // Additional code...
    } catch (Exception e) {
        // Handle exceptions...
    }
}

Question:

What could be causing this NoSuchFieldError related to the Factory in the context of Apache POI's XSSFWorkbook? Are there any known conflicts or compatibility issues with the mentioned dependencies in my pom.xml? Any insights or suggestions on how to resolve this issue would be greatly appreciated.

Thank you!

like image 255
Buddhika Ariyaratne Avatar asked Dec 28 '25 06:12

Buddhika Ariyaratne


1 Answers

That exception occurs when wrong version of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook is used. In versions of package ooxml-schemas-* this has sub class public static final class Factory while in versions of package poi-ooxml-full-5.* this has final class Factory. Thus sub classes org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook.Factory have different signatures.

Versions Apache POI 5 and higher need package poi-ooxml-full-5.* and cannot work using package ooxml-schemas-* anymore.

In your case the

<dependency>
    <groupId>fr.opensagres.xdocreport</groupId>
    <artifactId>org.apache.poi.xwpf.converter.xhtml</artifactId>
    <version>1.0.6</version>
</dependency>

pulls org.apache.poi.xwpf.converter.core-1.0.6 which pulls ooxml-schemas-1.1. That's wrong. Thus Apache POI 5 is incompatible with org.apache.poi.xwpf.converter.xhtml-1.0.6.

Try using

<dependency>
    <groupId>fr.opensagres.xdocreport</groupId>
    <artifactId>fr.opensagres.poi.xwpf.converter.xhtml</artifactId>
    <version>2.0.4</version>
</dependency>

instead. That pulls fr.opensagres.poi.xwpf.converter.core-2.0.4 which pulls poi-ooxml-full-5.2.0 then.

To be fully compatible with Apache POI 5.2.5, that should be poi-ooxml-full-5.2.5. So this should be added as dependency too.

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml-full</artifactId>
    <version>5.2.5</version>
</dependency>
like image 131
Axel Richter Avatar answered Dec 31 '25 00:12

Axel Richter