2

I have a project which involves writing to an Excel spreadsheet (.xlsx) using Apache POIs XSSFWorkbook() class.

It worked fine in a previous version of Apache POI for Java 8. Recently we migrated our code base to OpenJDK 11 and updated our Maven version for Apache POI to 4.1.0 as designated in the FAQ Apache POI FAQ.

Within Eclipse itself it is able to generate excel sheets to my Desktop as expected (on a Win 7 machine). However after doing a maven package into the jar file, the jar version does not work.

Am I compiling this wrong somehow? Snippet below (does not contain all the classes as there are a bunch of other classes which read in data from a different source)

private Workbook getWorkbook(String excelFilePath)
        throws IOException, EncryptedDocumentException, InvalidFormatException {
    Workbook workbook = null;

    if (excelFilePath.endsWith("xlsx")) {
        workbook = new XSSFWorkbook();
    //} else if (excelFilePath.endsWith("xls")) {
        //workbook = new HSSFWorkbook();
    } else {
        throw new IllegalArgumentException("The specified file is not Excel file");
    }

    return workbook;
}

public void writeReport(String excelFilePath) throws Exception {
    workbook = getWorkbook(excelFilePath);
    createHelper = workbook.getCreationHelper();

    //Summary Sheet
    TCOTSummarySheet summarySheet = new TCOTSummarySheet(workbook);
    summarySheet.setProject(PROJECT);
    summarySheet.setMaps(headerInfo, reportInfo, queryLinks);
    summarySheet.createSheet();

    //Bug Breakdown Sheet
    if (bugBreakdownFlag) {
        TCOTBugBreakdownSheet bugBreakdownSheet = new TCOTBugBreakdownSheet(workbook);
        bugBreakdownSheet.setSummarySheet(summarySheet);
        bugBreakdownSheet.setMaps(headerInfo, reportInfo, queryLinks);
        bugBreakdownSheet.createSheet();
    }


    //Complete Write
    try (FileOutputStream outputStream = new FileOutputStream(excelFilePath)) {
        workbook.write(outputStream);
    }
    catch (Exception e) {
        System.out.println(e.getLocalizedMessage());
    }
    workbook.close();
}

EDIT:

To give more context into the overarching project it is a JavaFX project written in OpenJDK 11.

This particular portion involves running a Swing application executed from JavaFX. This Swing application is actually the one using Apache POI to generate Excel Reports. Is this possibly due to a threading issue?

EDIT 2: Added picture of stack trace enter image description here

EDIT 3: I have tried adding javax dependencies to my pom.xml and also tried adding them as referenced external libraries but to no avail.

<!-- JAXB/Javax Dependencies -->
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.4.0-b180830.0359</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/javax.activation/javax.activation-api -->
    <dependency>
        <groupId>javax.activation</groupId>
        <artifactId>javax.activation-api</artifactId>
        <version>1.2.0</version>
    </dependency>

I've been compiling with maven package.

Chris Toh
  • 88
  • 1
  • 11
  • 1
    What error is thrown? Show stack trace please. – Axel Richter Feb 09 '20 at 18:36
  • Hi @AxelRichter, the interesting thing is I am not getting any stacktrace errors. I've even run the jar from cmd line to get console readings. It seems to silently fail on the .write() command but no actual stacktrace error is thrown. I attempted to add an explicit print via `try (FileOutputStream outputStream = new FileOutputStream(excelFilePath)) { workbook.write(outputStream); } catch (Exception e) { System.out.println(e.getLocalizedMessage()); }` But still got no stacktrace – Chris Toh Feb 09 '20 at 22:29
  • @AxelRichter after porting the entire swing application into JavaFX the issue is the same. It successfully generates an excel document from the Eclipse compiler but fails in the jar. However I was able to get java.lang.ExceptionInInitializerError. – Chris Toh Feb 12 '20 at 23:54
  • No help possible without knowing the full stack trace. Put all code in a `try { ... } catch (Throwable e) { e.printStackTrace(); }`. Then show the full stack trace thrown. – Axel Richter Feb 13 '20 at 07:19
  • @AxelRichter I added a picture of the stacktrace I was able to produce. It seems to be a cell merge issue but my question is why does this issue only occur in the compiled jar and not in Eclipse? – Chris Toh Feb 17 '20 at 02:17

1 Answers1

1

The issue was that the custom runtime image did not include jdk.charsets

jdk.charsets needs to be included when using the jlink application in creating runtimes used by apache poi.

Chris Toh
  • 88
  • 1
  • 11
  • Thank you for pointing out this issue. How did you deal with that missing *jdk.charsets*? What is the procedure to patch OpenJDK in the correct manner? – Martin Tovmassian Mar 17 '21 at 08:22
  • 1
    @MartinTovmassian This should help: https://stackoverflow.com/a/56807371/8659092. When creating a custom JRE in OpenJDK you will need to list the jdk.charsets module. Hope that helps! – Chris Toh Mar 17 '21 at 15:21