In the SpringBoot3 application built by Graalvm Native Image, I use Apache Poi to depend on log4j. The construction is successful, but the following errors occur at running. How can I configure it to avoid this error?
I tested in Spring Boot 2.7.1 + Spring Native, there is no problem.
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.0.0)
2022-12-06T11:53:05.855+08:00 INFO 18532 --- [ restartedMain] com.example.dataserver.Application : Starting AOT-processed Application using Java 17.0.5 with PID 18532 (D:\work-ide\IDEA\data-server\target\data-server.exe started by westinyang in D:\work-ide\IDEA\data-server\target)
2022-12-06T11:53:05.855+08:00 INFO 18532 --- [ restartedMain] com.example.dataserver.Application : No active profile set, falling back to 1 default profile: "default"
2022-12-06T11:53:05.855+08:00 INFO 18532 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2022-12-06T11:53:05.855+08:00 INFO 18532 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2022-12-06T11:53:05.889+08:00 INFO 18532 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2022-12-06T11:53:05.889+08:00 INFO 18532 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-12-06T11:53:05.889+08:00 INFO 18532 --- [ restartedMain] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.1]
2022-12-06T11:53:05.897+08:00 INFO 18532 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-12-06T11:53:05.897+08:00 INFO 18532 --- [ restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 41 ms
2022-12-06T11:53:05.920+08:00 INFO 18532 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2022-12-06T11:53:05.921+08:00 INFO 18532 --- [ restartedMain] com.example.dataserver.Application : Started Application in 0.072 seconds (process running for 0.078)
ERROR StatusLogger Unable to load services for service class org.apache.logging.log4j.spi.Provider
java.lang.InternalError: com.oracle.svm.core.jdk.UnsupportedFeatureError: Defining hidden classes at runtime is not supported.
at java.base@17.0.5/java.lang.invoke.InnerClassLambdaMetafactory.generateInnerClass(InnerClassLambdaMetafactory.java:413)
at java.base@17.0.5/java.lang.invoke.InnerClassLambdaMetafactory.spinInnerClass(InnerClassLambdaMetafactory.java:315)
at java.base@17.0.5/java.lang.invoke.InnerClassLambdaMetafactory.buildCallSite(InnerClassLambdaMetafactory.java:228)
at java.base@17.0.5/java.lang.invoke.LambdaMetafactory.metafactory(LambdaMetafactory.java:341)
at org.apache.logging.log4j.util.ServiceLoaderUtil.loadClassloaderServices(ServiceLoaderUtil.java:93)
at org.apache.logging.log4j.util.ServiceLoaderUtil.loadServices(ServiceLoaderUtil.java:74)
at org.apache.logging.log4j.util.ServiceLoaderUtil.loadServices(ServiceLoaderUtil.java:68)
at org.apache.logging.log4j.util.ProviderUtil.<init>(ProviderUtil.java:67)
at org.apache.logging.log4j.util.ProviderUtil.lazyInit(ProviderUtil.java:145)
at org.apache.logging.log4j.util.ProviderUtil.hasProviders(ProviderUtil.java:129)
at org.apache.logging.log4j.LogManager.<clinit>(LogManager.java:90)
at org.apache.poi.util.IOUtils.<clinit>(IOUtils.java:43)
at org.apache.poi.poifs.filesystem.FileMagic.valueOf(FileMagic.java:177)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:309)
at cn.hutool.poi.excel.WorkbookUtil.createBook(WorkbookUtil.java:120)
at cn.hutool.poi.excel.WorkbookUtil.createBook(WorkbookUtil.java:70)
at cn.hutool.poi.excel.ExcelReader.<init>(ExcelReader.java:71)
at cn.hutool.poi.excel.ExcelReader.<init>(ExcelReader.java:50)
at cn.hutool.poi.excel.ExcelUtil.getReader(ExcelUtil.java:151)
at cn.hutool.poi.excel.ExcelUtil.getReader(ExcelUtil.java:127)
at com.example.dataserver.AppRunner.run(AppRunner.java:23)
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:758)
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:748)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
at com.example.dataserver.Application.main(Application.java:13)
at java.base@17.0.5/java.lang.reflect.Method.invoke(Method.java:568)
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
at org.graalvm.nativeimage.builder/com.oracle.svm.core.thread.PlatformThreads.threadStartRoutine(PlatformThreads.java:775)
at org.graalvm.nativeimage.builder/com.oracle.svm.core.windows.WindowsPlatformThreads.osThreadStartRoutine(WindowsPlatformThreads.java:178)
Caused by: com.oracle.svm.core.jdk.UnsupportedFeatureError: Defining hidden classes at runtime is not supported.
at org.graalvm.nativeimage.builder/com.oracle.svm.core.util.VMError.unsupportedFeature(VMError.java:89)
at java.base@17.0.5/java.lang.ClassLoader.defineClass0(ClassLoader.java:338)
at java.base@17.0.5/java.lang.System$2.defineClass(System.java:2307)
at java.base@17.0.5/java.lang.invoke.MethodHandles$Lookup$ClassDefiner.defineClass(MethodHandles.java:2439)
at java.base@17.0.5/java.lang.invoke.MethodHandles$Lookup$ClassDefiner.defineClassAsLookup(MethodHandles.java:2420)
at java.base@17.0.5/java.lang.invoke.MethodHandles$Lookup.defineHiddenClass(MethodHandles.java:2127)
at java.base@17.0.5/java.lang.invoke.InnerClassLambdaMetafactory.generateInnerClass(InnerClassLambdaMetafactory.java:407)
... 28 more
ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...
2022-12-06T11:53:05.981+08:00 INFO 18532 --- [ restartedMain] com.example.dataserver.AppRunner : 3
2022-12-06T11:53:05.981+08:00 INFO 18532 --- [ restartedMain] com.example.dataserver.AppRunner : Service running on 127.0.0.1:8080
I tried some solutions, but there is no effect yet,I joined the native-image parameter --initialize-at-run-time=org.apache.logging.log4j.spi.Provider
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
<configuration>
<buildArgs>
<arg>--no-fallback</arg>
<arg>--initialize-at-run-time=org.apache.logging.log4j.spi.Provider</arg>
<!-- org.apache.poi: Caused by: java.nio.charset.UnsupportedCharsetException: CP1252 -->
<arg>-H:+AddAllCharsets</arg>
</buildArgs>
</configuration>
</plugin>
Comment From: mhalbritter
Looks like that Apache POI is not native-image compatible. Please create an issue on https://github.com/oracle/graalvm-reachability-metadata, thanks!
Comment From: westinyang
However, I tested in Spring Boot 2.7.1 + Spring Native, no problem.
Comment From: oeresundsgruppen
Have this with springboot 3.0.1, also Unable to load services for service class org.apache.logging.log4j.spi.Provider
Comment From: scottfrederick
However, I tested in Spring Boot 2.7.1 + Spring Native, no problem.
@westinyang Spring Native provided native hints for many third-party libraries. It's likely that there are hints in there that made your setup work.
It is not reasonable for the Spring team to provide native hints for libraries that we don't maintain ourselves. Instead, those hints belong in the libraries or in GraalVM Reachability Metadata Repository that anyone can contribute to. This is why Moritz suggested you should open an issue in the reachability repo project instead of here.
Comment From: oeresundsgruppen
Seems log4j is not working with native !!! https://issues.apache.org/jira/browse/LOG4J2-2604
Comment From: chuailiwu
I met the same problem. How did it finally be solved?
Comment From: wilkinsona
@chuailiwu As far as we know, the problems discussed above have not yet been solved. We believe some changes in Log4j 2 are required. https://issues.apache.org/jira/browse/LOG4J2-2604 is tracking that and it is out of Spring Boot's control.
Comment From: chuailiwu
@wilkinsona thx! Log4j2 replaced to logback is ok now.
Comment From: imgoby
@wilkinsona thx! Log4j2 replaced to logback is ok now.
@chuailiwu Could you explain how to config? I meet the same issue.
Comment From: vcvitaly
@wilkinsona thx! Log4j2 replaced to logback is ok now.
@chuailiwu Could you explain how to config? I meet the same issue.
I encountered the same issue it seems. Followed this article - https://www.baeldung.com/logback
Comment From: Emmerson-Miranda
Thanks guys, replace log4j dependencies for logback fixed the issue.
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.5.6</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.5.6</version>
</dependency>