spring-attic / spring-native

Spring Native is now superseded by Spring Boot 3 official native support
https://docs.spring.io/spring-boot/docs/current/reference/html/native-image.html
Apache License 2.0
2.74k stars 355 forks source link

ClassNotFoundException: org.springframework.aot.ContextBootstrapInitializer #1695

Closed binglien closed 1 year ago

binglien commented 2 years ago

Hi folks,

I built my spring boot native images on macOs and Linux, but I executed the native images, then I got the following error both:

Exception in thread "main" java.lang.IllegalStateException: java.lang.ClassNotFoundException: org.springframework.aot.ContextBootstrapInitializer. This exception was synthesized during native image building from a call to java.lang.Class.forName(String) with constant arguments.
    at org.springframework.aot.SpringApplicationAotUtils.getBootstrapInitializer(SpringApplicationAotUtils.java:41)
    at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:133)
    at org.springframework.boot.builder.SpringApplicationBuilder.createSpringApplication(SpringApplicationBuilder.java:129)
    at org.springframework.boot.builder.SpringApplicationBuilder.<init>(SpringApplicationBuilder.java:102)
    at org.springframework.boot.builder.SpringApplicationBuilder.<init>(SpringApplicationBuilder.java:98)
    at com.hitrust.mgear.demo.msecure.springboot.MSecureConsumerApplication.main(MSecureConsumerApplication.java:38)
Caused by: java.lang.ClassNotFoundException: org.springframework.aot.ContextBootstrapInitializer. This exception was synthesized during native image building from a call to java.lang.Class.forName(String) with constant arguments.
    at org.springframework.aot.SpringApplicationAotUtils.getBootstrapInitializer(SpringApplicationAotUtils.java:38)
    ... 5 more

GraalVM 22.2.0 Java 17 CE spring-boot 2.7.2 spring-aot-maven-plugin 0.12.1 spring-native 0.12.1 org.graalvm.buildtools:native-maven-plugin:0.9.13

            <plugin>
                <groupId>org.graalvm.buildtools</groupId>
                <artifactId>native-maven-plugin</artifactId>
                <version>${native.maven.plugin.version}</version>
                <extensions>true</extensions>
                <executions>
                    <execution>
                        <id>build-native</id>
                        <goals>
                            <goal>build</goal>
                        </goals>
                        <phase>package</phase>
                    </execution>
                </executions>
                <configuration>
                    <skip>${native.skip}</skip>
                    <useArgFile>false</useArgFile>
                    <imageName>${project.artifactId}</imageName>
                    <buildArgs>
                        <buildArg>-H:IncludeResources=META-INF/spring.factories|org/springframework/boot/logging/.*</buildArg>
                        <buildArg>-H:ReflectionConfigurationFiles=${project.basedir}/app.json</buildArg>
                        <buildArg>--no-fallback</buildArg>
                        <buildArg>--report-unsupported-elements-at-runtime</buildArg>
                    </buildArgs>
                    <classpath>
                        <param>${project.build.directory}/${project.artifactId}-${project.version}.jar</param>
                        <param>${maven.classpath}</param>
                    </classpath>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.7</version>
                <executions>
                    <execution>
                        <id>define-classpath</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <exportAntProperties>true</exportAntProperties>
                            <target>
                                <property name="maven.classpath" refid="maven.runtime.classpath" />
                            </target>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.graalvm.buildtools</groupId>
                <artifactId>native-maven-plugin</artifactId>
                <version>${native.maven.plugin.version}</version>
                <configuration>
                    <skip>${native.skip}</skip>
                    <imageName>${project.artifactId}</imageName>
                    <mainClass>${jib.mainClass}</mainClass>
                    <buildArgs>
                        <buildArg>-H:+ReportExceptionStackTraces</buildArg>
                        <buildArg>-H:+TraceNativeToolUsage</buildArg>
                        <buildArg>--allow-incomplete-classpath </buildArg>
                    </buildArgs>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>build</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.springframework.experimental</groupId>
                <artifactId>spring-aot-maven-plugin</artifactId>
                <version>0.12.1</version>
                <executions>
                    <execution>
                        <id>generate</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>test-generate</id>
                        <goals>
                            <goal>test-generate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
sdeleuze commented 2 years ago

Please share a repro project.

spring-projects-issues commented 2 years ago

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

binglien commented 2 years ago

Please share a repro project. OK

https://github.com/binglien/native-hsm-poc/tree/main/msecure-graalvm

binglien commented 2 years ago

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

provided, thank you.

sdeleuze commented 2 years ago

I get a Could not find artifact com.hitrust.mgear.demo:faas-hsm-poc:pom:5.0.0-SNAPSHOT error, please update the repro project.

spring-projects-issues commented 2 years ago

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

binglien commented 2 years ago

I get a Could not find artifact com.hitrust.mgear.demo:faas-hsm-poc:pom:5.0.0-SNAPSHOT error, please update the repro project.

It's my fault, updated pom.xml. thank you.

binglien commented 2 years ago

Reproduce steps:

$ ./mvnw -Djib.skip=true clean package
...
Finished generating 'msecure-native-webflux' in 13m 3s.
[INFO] 
[INFO] --- spring-boot-maven-plugin:2.7.2:repackage (default) @ msecure-native-webflux ---
[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for native-hsm-poc 5.0.0-SNAPSHOT:
[INFO] 
[INFO] native-hsm-poc ..................................... SUCCESS [  0.223 s]
[INFO] msecure-springboot-webflux ......................... SUCCESS [  7.559 s]
[INFO] msecure-springboot ................................. SUCCESS [  1.044 s]
[INFO] msecure-graalvm .................................... SUCCESS [  0.178 s]
[INFO] msecure-native ..................................... SUCCESS [  6.681 s]
[INFO] msecure-native-consumer ............................ SUCCESS [25:14 min]
[INFO] msecure-natvie-webflux ............................. SUCCESS [27:01 min]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  52:33 min
[INFO] Finished at: 2022-09-07T23:24:42-04:00
[INFO] ------------------------------------------------------------------------

$ cd msecure-graalvm/msecure-native/msecure-native-consumer/target
$ ./msecure-native-consumer 

23:45:35.772 [main] INFO com.hitrust.mgear.demo.msecure.springboot.MSecureConsumerApplication - Starting Consumer mSecure application...
Exception in thread "main" java.lang.IllegalStateException: java.lang.ClassNotFoundException: org.springframework.aot.ContextBootstrapInitializer. This exception was synthesized during native image building from a call to java.lang.Class.forName(String) with constant arguments.
    at org.springframework.aot.SpringApplicationAotUtils.getBootstrapInitializer(SpringApplicationAotUtils.java:41)
    at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:133)
    at org.springframework.boot.builder.SpringApplicationBuilder.createSpringApplication(SpringApplicationBuilder.java:129)
    at org.springframework.boot.builder.SpringApplicationBuilder.<init>(SpringApplicationBuilder.java:102)
    at org.springframework.boot.builder.SpringApplicationBuilder.<init>(SpringApplicationBuilder.java:98)
    at com.hitrust.mgear.demo.msecure.springboot.MSecureConsumerApplication.main(MSecureConsumerApplication.java:46)
Caused by: java.lang.ClassNotFoundException: org.springframework.aot.ContextBootstrapInitializer. This exception was synthesized during native image building from a call to java.lang.Class.forName(String) with constant arguments.
    at org.springframework.aot.SpringApplicationAotUtils.getBootstrapInitializer(SpringApplicationAotUtils.java:38)
    ... 5 more
sdeleuze commented 1 year ago

It is probably related to the fact Spring Native is only searching the main class in directories not JAR, see this related comment. This limitation will be remove in Spring Boot 3.

On Spring Native side, you should be able to make it work by configuring explicitly the main class at Spring AOT plugin level, see https://docs.spring.io/spring-native/docs/current/reference/htmlsingle/#aot-build-setup-configuration related documentation. You can search No application class detected, skipping context bootstrap in the logs to see if the application class is found or not.