spring-projects / spring-projects/spring-ai

Native image compilation issue with spring-ai-tika-document-reader dependency

Open
#779 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

aot
Dominant language
Java
Stars
9.5k
Forks
2.9k
Avg merge
1d 7h
Merged PRs (30d)
6

Description

I am trying to build a native image of a Spring AI based application using Native Build tools and am getting an error while building the native image. The error is due to the Spring AI Tika document reader dependency, as I am only getting this error when I have this dependency in the pom.xml.

Native image version used -

native-image 21.0.3 2024-04-16
GraalVM Runtime Environment Liberica-NIK-23.1.3-1 (build 21.0.3+10-LTS)
Substrate VM Liberica-NIK-23.1.3-1 (build 21.0.3+10-LTS, serial gc)

Error during native image build -

[2/8] Performing analysis...  [*]                                                                       (45.6s @ 3.92GB)
   43,333 reachable types   (90.2% of   48,052 total)
   79,502 reachable fields  (65.7% of  120,934 total)
  206,455 reachable methods (61.7% of  334,499 total)
   12,065 types, 1,221 fields, and 10,118 methods registered for reflection
       12 native libraries: -framework Accelerate, -framework Carbon, -framework Cocoa, -framework CoreServices, -framework JavaRuntimeSupport, -framework Metal, -framework OpenGL, -framework QuartzCore, -framework Security, m, stdc++

Error: Classes that should be initialized at run time got initialized during image building:
 org.apache.commons.logging.LogFactory was unintentionally initialized at build time. To see why org.apache.commons.logging.LogFactory got initialized use --trace-class-initialization=org.apache.commons.logging.LogFactory
org.apache.commons.logging.impl.SLF4JLogFactory was unintentionally initialized at build time. To see why org.apache.commons.logging.impl.SLF4JLogFactory got initialized use --trace-class-initialization=org.apache.commons.logging.impl.SLF4JLogFactory
To see how the classes got initialized, use --trace-class-initialization=org.apache.commons.logging.LogFactory,org.apache.commons.logging.impl.SLF4JLogFactory

I traced the initialisation of these classes and noticed this in the logs when I tried to build the native-image -

Error: Classes that should be initialized at run time got initialized during image building:
 org.apache.commons.logging.impl.SLF4JLogFactory was unintentionally initialized at build time. org.springframework.beans.factory.support.DisposableBeanAdapter caused initialization of this class with the following trace: 
	at org.apache.commons.logging.impl.SLF4JLogFactory.<clinit>(SLF4JLogFactory.java)
	at jdk.internal.misc.Unsafe.ensureClassInitialized0(Unknown Source)
	at jdk.internal.misc.Unsafe.ensureClassInitialized(Unsafe.java:1160)
	at jdk.internal.reflect.MethodHandleAccessorFactory.ensureClassInitialized(MethodHandleAccessorFactory.java:300)
	at jdk.internal.reflect.MethodHandleAccessorFactory.newConstructorAccessor(MethodHandleAccessorFactory.java:103)
	at jdk.internal.reflect.ReflectionFactory.newConstructorAccessor(ReflectionFactory.java:200)
	at java.lang.reflect.Constructor.acquireConstructorAccessor(Constructor.java:549)
	at java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
	at java.lang.reflect.ReflectAccess.newInstance(ReflectAccess.java:128)
	at jdk.internal.reflect.ReflectionFactory.newInstance(ReflectionFactory.java:304)
	at java.lang.Class.newInstance(Class.java:725)
	at org.apache.commons.logging.LogFactory.createFactory(LogFactory.java:1047)
	at org.apache.commons.logging.LogFactory$2.run(LogFactory.java:960)
	at java.security.AccessController.executePrivileged(AccessController.java:778)
	at java.security.AccessController.doPrivileged(AccessController.java:319)
	at org.apache.commons.logging.LogFactory.newFactory(LogFactory.java:957)
	at org.apache.commons.logging.LogFactory.getFactory(LogFactory.java:552)
	at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:655)
	at org.springframework.beans.factory.support.DisposableBeanAdapter.<clinit>(DisposableBeanAdapter.java:76)

org.apache.commons.logging.LogFactory was unintentionally initialized at build time. org.springframework.beans.factory.support.DisposableBeanAdapter caused initialization of this class with the following trace: 
	at org.apache.commons.logging.LogFactory.<clinit>(LogFactory.java:136)
	at jdk.internal.misc.Unsafe.ensureClassInitialized0(Unknown Source)
	at jdk.internal.misc.Unsafe.ensureClassInitialized(Unsafe.java:1160)
	at jdk.internal.reflect.MethodHandleAccessorFactory.ensureClassInitialized(MethodHandleAccessorFactory.java:300)
	at jdk.internal.reflect.MethodHandleAccessorFactory.newConstructorAccessor(MethodHandleAccessorFactory.java:103)
	at jdk.internal.reflect.ReflectionFactory.newConstructorAccessor(ReflectionFactory.java:200)
	at java.lang.reflect.Constructor.acquireConstructorAccessor(Constructor.java:549)
	at java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
	at java.lang.reflect.ReflectAccess.newInstance(ReflectAccess.java:128)
	at jdk.internal.reflect.ReflectionFactory.newInstance(ReflectionFactory.java:304)
	at java.lang.Class.newInstance(Class.java:725)
	at org.apache.commons.logging.LogFactory.createFactory(LogFactory.java:1047)
	at org.apache.commons.logging.LogFactory$2.run(LogFactory.java:960)
	at java.security.AccessController.executePrivileged(AccessController.java:778)
	at java.security.AccessController.doPrivileged(AccessController.java:319)
	at org.apache.commons.logging.LogFactory.newFactory(LogFactory.java:957)
	at org.apache.commons.logging.LogFactory.getFactory(LogFactory.java:552)
	at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:655)
	at org.springframework.beans.factory.support.DisposableBeanAdapter.<clinit>(DisposableBeanAdapter.java:76)

I also tried registering hints for reflection but couldn't get the application working.
Is there a way to resolve this issue?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the application's pom.xml and reproduce the native-image build using the Spring AI Tika document reader dependency. Use the reported --trace-class-initialization options for org.apache.commons.logging.LogFactory and SLF4JLogFactory; done means the native image builds successfully with that dependency present.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
build-system
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.