Search code examples
kotlinapache-poiquarkusgraalvmgraalvm-native-image

Class Cast Exception in Kotlin Quarkus GraalVM Native Image


SOLVED: @geoand mentioned the io.quarkiverse.poi:quarkus-poi extension in the comments, which solved all my problems! See: https://github.com/quarkiverse/quarkus-poi and: https://mvnrepository.com/artifact/io.quarkiverse.poi/quarkus-poi/2.0.2

I’m trying to create a microservices that responds with an excel file. If I run quarkusDev everything works fine. But after building the native image, I’m getting this error:

    2023-07-06 15:54:30,451 ERROR [io.qua.ver.htt.run.QuarkusErrorHandler] (executor-thread-1) HTTP    Request to /api/excel failed, error id: 0694d603-5557-4806-bd87-3cd93c6730d9-2:   java.lang.ClassCastException: org.apache.xmlbeans.impl.values.XmlComplexContentImpl cannot be cast   to org.openxmlformats.schemas.drawingml.x2006.main.ThemeDocument
        at org.apache.poi.xssf.model.ThemesTable.readFrom(ThemesTable.java:119)
        at org.apache.poi.xssf.model.ThemesTable.<init>(ThemesTable.java:87)
        at org.apache.poi.ooxml.POIXMLFactory.createDocumentPart(POIXMLFactory.java:61)
        at org.apache.poi.ooxml.POIXMLDocumentPart.read(POIXMLDocumentPart.java:661)
        at org.apache.poi.ooxml.POIXMLDocument.load(POIXMLDocument.java:165)
        at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:260)
        at   org.apache.poi.xssf.usermodel.XSSFWorkbookFactory.createWorkbook(XSSFWorkbookFactory.java:118)
        at org.apache.poi.xssf.usermodel.XSSFWorkbookFactory.create(XSSFWorkbookFactory.java:98)
        at org.apache.poi.xssf.usermodel.XSSFWorkbookFactory.create(XSSFWorkbookFactory.java:36)
        at org.apache.poi.ss.usermodel.WorkbookFactory.lambda$create$2(WorkbookFactory.java:224)
        at org.apache.poi.ss.usermodel.WorkbookFactory.wp(WorkbookFactory.java:329)
        at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:224)
        at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:185)
        at com.test.services.ExcelService.createExcel(ExcelService.kt:13)
        at com.test.services.ExcelService_ClientProxy.createExcel(Unknown Source)
        at com.test.services.ExcelFileService.submitDTO(ExcelFileService.kt:39)
        at com.test.services.ExcelFileService_ClientProxy.submitDTO(Unknown Source)
        at com.test.resources.ExcelFileResource.DTO(ExcelFileResource.kt:79)
        at com.test.resources.ExcelFileResource_Subclass.DTO$$superforward(Unknown Source)
        at com.test.resources.ExcelFileResource_Subclass$$function$$2.apply(Unknown Source)
        at io.quarkus.arc.impl.AroundInvokeInvocationContext.proceed(AroundInvokeInvocationContext.java:73)
        at io.quarkus.arc.impl.AroundInvokeInvocationContext.proceed(AroundInvokeInvocationContext.java:62)
        at io.quarkus.hibernate.validator.runtime.interceptor.AbstractMethodValidationInterceptor.validateMethodInvocation(AbstractMethodValidationInterceptor.java:71)
        at io.quarkus.hibernate.validator.runtime.jaxrs.ResteasyReactiveEndPointValidationInterceptor.validateMethodInvocation(ResteasyReactiveEndPointValidationInterceptor.java:21)
        at   io.quarkus.hibernate.validator.runtime.jaxrs.ResteasyReactiveEndPointValidationInterceptor_Bean.intercept(Unknown Source)
        at io.quarkus.arc.impl.InterceptorInvocation.invoke(InterceptorInvocation.java:42)
        at io.quarkus.arc.impl.AroundInvokeInvocationContext.perform(AroundInvokeInvocationContext.java:30)
        at io.quarkus.arc.impl.InvocationContexts.performAroundInvoke(InvocationContexts.java:27)
        at com.test.resources.ExcelFileResource_Subclass.DTO(Unknown Source)
        at com.test.resources.ExcelFileResource$quarkusrestinvoker$DTO_d3f979f0d6dd3140a96a1564a9bba43107110fd0.invoke(Unknown Source)
        at org.jboss.resteasy.reactive.server.handlers.InvocationHandler.handle(InvocationHandler.java:29)
        at io.quarkus.resteasy.reactive.server.runtime.Q`your text`uarkusResteasyReactiveRequestContext.invokeHandler(QuarkusResteasyReactiveRequestContext.java:141)
        at org.jboss.resteasy.reactive.common.core.AbstractResteasyReactiveContext.run(AbstractResteasyReactiveContext.java:145)
        at io.quarkus.vertx.core.runtime.VertxCoreRecorder$14.runWith(VertxCoreRecorder.java:576)
        at org.jboss.threads.EnhancedQueueExecutor$Task.run(EnhancedQueueExecutor.java:2513)
        at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1538)
        at org.jboss.threads.DelegatingRunnable.run(DelegatingRunnable.java:29)
        at org.jboss.threads.ThreadLocalResettingRunnable.run(ThreadLocalResettingRunnable.java:29)
        at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
        at [email protected]/java.lang.Thread.run(Thread.java:833)
        at org.graalvm.nativeimage.builder/com.oracle.svm.core.thread.PlatformThreads.threadStartRoutine(PlatformThreads.java:775)
        at org.graalvm.nativeimage.builder/com.oracle.svm.core.posix.thread.PosixPlatformThreads.pthreadStartRoutine(PosixPlatformThreads.java:203)

This is the function for the xlsx creation:

        fun createExcel(_: List<Input>): ByteArrayOutputStream {
            val inputStream = this::class.java.classLoader.getResourceAsStream("excel-templates/test.xlsx")
            val workbook = WorkbookFactory.create(inputStream)
    
            val sheet = workbook.getSheetAt(0)
            val row = sheet.getRow(0)
            val cell = row.createCell(1)
            cell.setCellValue("new entry")
    
            val baos = ByteArrayOutputStream()
            workbook.write(baos)
            baos.close()
    
            return baos
        }

These are the dependencies:

    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    implementation("io.quarkus:quarkus-kotlin")
    implementation("io.quarkus:quarkus-arc")
    implementation("io.quarkus:quarkus-resteasy-reactive-jackson")
    implementation("io.quarkus:quarkus-rest-client-reactive-jackson")
    implementation("io.quarkus:quarkus-smallrye-openapi")
    implementation("io.quarkus:quarkus-hibernate-validator")
    //POI
    implementation("org.apache.poi:poi:5.2.3")
    implementation("org.apache.poi:poi-ooxml:5.2.3")
    implementation("org.apache.poi:poi-ooxml-full:5.2.3")
    //Log4j
    implementation("org.apache.logging.log4j:log4j-api:2.20.0")
    implementation("org.apache.logging.log4j:log4j-core:2.20.0")

Using: Quarkus v3.1.2.Final Build with: ./gradlew clean build -Dquarkus.package.type=native -Dquarkus.native.container-build=true Image: quay.io/quarkus/ubi-quarkus-mandrel-builder-image:22.3-java17

I’ve also tried to build the "problematic" dependencies at build time (as a additional build arg in the application.properites) but with no effect


Solution

  • You most likely need to add the Apache POI extension which does a few things in order to make POI work on native mode.