Closed cosminpolifronie closed 9 months ago
Simillar to #1993 but this uses Java 11.
I see that you attached the native-image\META-INF\native-image\reflect-config.json
file and that it contains org.apache.xmlbeans.impl.schema.SchemaTypeLoaderImpl
but I don't see how you specify this file to native-image
. I think that in this case, since you don't package your app as a jar, you need to specify the reflection config explicitly, I don't think it is automatically detected in META-INF
. @jramirez-isc can you please try to reproduce? Thanks!
@sgyeme in my experience, the ClassCastException is always caused by missing reflection configuration. For yours specifically, org.openxmlformats.schemas.spreadsheetml.x2006.main.impl.CTWorkbookImpl
and its constructor.
I'll also add that you can initialize org.apache.commons.compress
at build-time, which is the only config I have for apache commons-compress. The reflection config is afaik only required for the static initialization of org.apache.commons.compress.archivers.zip.ExtraFieldUtils
so putting it (and the whole package) at build-time is much easier.
For this issue itself: Other than that, you have a few org.apache.xmlbeans classes in your config that I didn't need, but that's probably required for the .toString call.
The config itself looks ok, and for me, the agent was able to get all the config I needed. (I also use XSSFWorkbook, so I don't see any reason why that would not be the case for you). If the suggestion from @cstancu doesn't work, you may want to try POI 5.0.0, I have since moved on to it and I find it a tiny bit more native-image friendly, although most of the config will stay the same.
@kkriske could you please share the configuration you have for org.apache.commons.compress
?
As stated in my previous message, just initializing everything at build-time:
# native-image.properties
Args = --initialize-at-build-time=\
org.apache.commons.compress
I'm currently using commons-compress:1.20
.
That's all, but fair warning, initializing entire packages at built-time instead of letting GraalVM detect what is safe to initialize is discouraged as it may introduce hard to debug bugs.
As far as I know, only org.apache.commons.compress.archivers.zip.ExtraFieldUtils
would require reflection config if initialized at run-time, so if you want to keep the previous warning in mind, you may want to only initialize this class at build-time and let GraalVM figure out the rest.
Are you running into any specific issues with commons-compress?
@kkriske thank you for the prompt response. I am in the process of migrating some microservices to native (using poi-ooxml 5.2.0), and I get the following error during Runtime:
2022-04-06T11:15:56.642066+00:00 app[web.1]: Caused by: java.lang.ExceptionInInitializerError
2022-04-06T11:15:56.642076+00:00 app[web.1]: at org.apache.commons.compress.archivers.zip.ZipArchiveEntry.setExtra(ZipArchiveEntry.java:713)
2022-04-06T11:15:56.642087+00:00 app[web.1]: at org.apache.commons.compress.archivers.zip.ZipArchiveInputStream.getNextZipEntry(ZipArchiveInputStream.java:350)
2022-04-06T11:15:56.642101+00:00 app[web.1]: at org.apache.poi.openxml4j.util.ZipArchiveThresholdInputStream.getNextEntry(ZipArchiveThresholdInputStream.java:152)
2022-04-06T11:15:56.642110+00:00 app[web.1]: at org.apache.poi.openxml4j.util.ZipInputStreamZipEntrySource.<init>(ZipInputStreamZipEntrySource.java:94)
2022-04-06T11:15:56.642120+00:00 app[web.1]: at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:132)
2022-04-06T11:15:56.642131+00:00 app[web.1]: at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:312)
2022-04-06T11:15:56.642140+00:00 app[web.1]: at org.apache.poi.ooxml.util.PackageHelper.open(PackageHelper.java:59)
2022-04-06T11:15:56.642150+00:00 app[web.1]: at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:304)
2022-04-06T11:15:56.642158+00:00 app[web.1]: at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:300)
2022-04-06T11:15:56.642170+00:00 app[web.1]: at gr.kmandalas.expenser.service.ExcelService.generateExcel(ExcelService.java:55)
2022-04-06T11:15:56.642179+00:00 app[web.1]: at gr.kmandalas.expenser.controller.ExpensesController.generate(ExpensesController.java:72)
2022-04-06T11:15:56.642189+00:00 app[web.1]: at java.lang.reflect.Method.invoke(Method.java:566)
2022-04-06T11:15:56.642198+00:00 app[web.1]: at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
2022-04-06T11:15:56.642210+00:00 app[web.1]: at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150)
2022-04-06T11:15:56.642220+00:00 app[web.1]: at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117)
2022-04-06T11:15:56.642229+00:00 app[web.1]: at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
2022-04-06T11:15:56.642240+00:00 app[web.1]: at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808)
2022-04-06T11:15:56.642250+00:00 app[web.1]: at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
2022-04-06T11:15:56.642258+00:00 app[web.1]: at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1067)
2022-04-06T11:15:56.642268+00:00 app[web.1]: ... 86 more
2022-04-06T11:15:56.642282+00:00 app[web.1]: Caused by: java.lang.RuntimeException: class org.apache.commons.compress.archivers.zip.AsiExtraField is not a concrete class
2022-04-06T11:15:56.642292+00:00 app[web.1]: at org.apache.commons.compress.archivers.zip.ExtraFieldUtils.register(ExtraFieldUtils.java:73)
2022-04-06T11:15:56.642301+00:00 app[web.1]: at org.apache.commons.compress.archivers.zip.ExtraFieldUtils.<clinit>(ExtraFieldUtils.java:43)
2022-04-06T11:15:56.642310+00:00 app[web.1]: ... 105 more
UPDATE: After adding the --initialize-at-build-time=org.apache.commons.compress
hint, I get now a different error:
2022-04-07T08:36:46.256635+00:00 app[web.1]: Caused by: java.lang.ExceptionInInitializerError
2022-04-07T08:36:46.256645+00:00 app[web.1]: at org.openxmlformats.schemas.spreadsheetml.x2006.main.ExternalLinkDocument.<clinit>(ExternalLinkDocument.java:23)
2022-04-07T08:36:46.256655+00:00 app[web.1]: at org.apache.poi.xssf.model.ExternalLinksTable.readFrom(ExternalLinksTable.java:66)
2022-04-07T08:36:46.256665+00:00 app[web.1]: at org.apache.poi.xssf.model.ExternalLinksTable.<init>(ExternalLinksTable.java:60)
2022-04-07T08:36:46.256680+00:00 app[web.1]: at org.apache.poi.ooxml.POIXMLFactory.createDocumentPart(POIXMLFactory.java:61)
2022-04-07T08:36:46.256689+00:00 app[web.1]: at org.apache.poi.ooxml.POIXMLDocumentPart.read(POIXMLDocumentPart.java:661)
2022-04-07T08:36:46.256699+00:00 app[web.1]: at org.apache.poi.ooxml.POIXMLDocument.load(POIXMLDocument.java:165)
2022-04-07T08:36:46.256709+00:00 app[web.1]: at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:274)
2022-04-07T08:36:46.256719+00:00 app[web.1]: at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:304)
2022-04-07T08:36:46.256728+00:00 app[web.1]: at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:300)
2022-04-07T08:36:46.256738+00:00 app[web.1]: at gr.kmandalas.expenser.service.ExcelService.generateExcel(ExcelService.java:55)
2022-04-07T08:36:46.256748+00:00 app[web.1]: at gr.kmandalas.expenser.controller.ExpensesController.generate(ExpensesController.java:72)
2022-04-07T08:36:46.256758+00:00 app[web.1]: at java.lang.reflect.Method.invoke(Method.java:566)
2022-04-07T08:36:46.256769+00:00 app[web.1]: at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
2022-04-07T08:36:46.256779+00:00 app[web.1]: at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150)
2022-04-07T08:36:46.256794+00:00 app[web.1]: at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117)
2022-04-07T08:36:46.256805+00:00 app[web.1]: at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
2022-04-07T08:36:46.256815+00:00 app[web.1]: at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808)
2022-04-07T08:36:46.256829+00:00 app[web.1]: at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
2022-04-07T08:36:46.256838+00:00 app[web.1]: at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1067)
2022-04-07T08:36:46.256848+00:00 app[web.1]: ... 86 more
2022-04-07T08:36:46.256870+00:00 app[web.1]: Caused by: org.apache.xmlbeans.SchemaTypeLoaderException: XML-BEANS compiled schema: Could not locate compiled schema resource org/apache/poi/schemas/ooxml/system/ooxml/index.xsb (org.apache.poi.schemas.ooxml.system.ooxml.index) - code 0
2022-04-07T08:36:46.256872+00:00 app[web.1]: at org.apache.xmlbeans.impl.schema.XsbReader.<init>(XsbReader.java:63)
2022-04-07T08:36:46.256887+00:00 app[web.1]: at org.apache.xmlbeans.impl.schema.SchemaTypeSystemImpl.initFromHeader(SchemaTypeSystemImpl.java:235)
2022-04-07T08:36:46.256888+00:00 app[web.1]: at org.apache.xmlbeans.impl.schema.SchemaTypeSystemImpl.<init>(SchemaTypeSystemImpl.java:201)
2022-04-07T08:36:46.256932+00:00 app[web.1]: at org.apache.poi.schemas.ooxml.system.ooxml.TypeSystemHolder.<init>(TypeSystemHolder.java:9)
2022-04-07T08:36:46.256934+00:00 app[web.1]: at org.apache.poi.schemas.ooxml.system.ooxml.TypeSystemHolder.<clinit>(TypeSystemHolder.java:6)
2022-04-07T08:36:46.256935+00:00 app[web.1]: ... 105 more
In my pom.xml, I only have the following dependency:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.0</version>
</dependency>
POI requires resources & reflection at runtime so you need to provide the config for that. I've previously explained a bit about POI and native-image here: https://github.com/oracle/graal/issues/3191 But in short, the resource & reflection config you need can be generated with the native-image-agent.
@kkriske I finally managed to resolve the issues by using the native-image-agent. Thanks for the feedback.
@kkriske I am using an Apache POI lib in Micronaut project which I am building a Graalvm native-image. But the issue is there are tons of classes and method in POI lib which are using the reflection, and POI dynamically uses any of those classes to read the excel file. I am developing an application where end users will upload and read the excel files, and thus I will need the reflection configs of all the classes and method of POI lib. How can I get those configs without actually executing that code?
native-image-agent ?
Could you explain ? Which command do you run ? please
Using the native-image-agent
seems to have fixed the issue.
Describe GraalVM and your environment :
java -Xinternalversion
:Have you verified this issue still happens when using the latest snapshot? It has been verified using GraalVM CE 20.1.0-dev-20200225_0946, the latest snapshot at the time of reporting.
Describe the issue The example application instantiates an Apache POI class. Its dependencies use reflection, so a reflection config has to be generated for the native-image build. When running the built executable, the following exception occurs:
Describe the full native-image command
Code snippet or code repository that reproduces the issue
Steps to reproduce the issue
graalvm_ni_apachepoi_failure
and pastesrc\main\java\graalvm_ni_apachepoi_failure\App.java
,build.gradle
,native-image\META-INF\native-image\jni-config.json
,native-image\META-INF\native-image\proxy-config.json
,native-image\META-INF\native-image\reflect-config.json
,native-image\META-INF\native-image\resource-config.json
provided above into itgradle clean build
into thegraalvm_ni_apachepoi_failure
directorygu install native-image
graalvm_ni_apachepoi_failure\native-image
java -agentlib:native-image-agent=config-output-dir=META-INF\native-image -cp "..\build\distributions\graalvm_ni_apachepoi_failure\lib\*" graalvm_ni_apachepoi_failure.App
native-image -cp "..\build\distributions\graalvm_ni_apachepoi_failure\lib\*" --no-fallback --verbose graalvm_ni_apachepoi_failure.App
Expected behavior The application should have printed the string representation of a XSSFWorkbook object.