Main:

fun main(args: Array<String>) {
    SpringApplicationBuilder(ServerContextConfiguration::class.java).web(WebApplicationType.NONE)
        .child(ApiContextConfiguration::class.java).web(WebApplicationType.SERVLET)
        .sibling(WebContextConfiguration::class.java).web(WebApplicationType.SERVLET)
        .run(*args)
}

Command

gradlew nativeRun

Description: I encountered an issue while trying to start my native web application built with Spring Boot and GraalVM. The application failed to start, and the following error message was displayed:

Log:

Starting a Gradle Daemon, 1 incompatible and 2 stopped Daemons could not be reused, use --status for details
> Task :core:processResources NO-SOURCE
> Task :api:processResources NO-SOURCE
> Task :web:processResources NO-SOURCE
> Task :processResources
> Task :core:compileKotlin
> Task :core:compileJava NO-SOURCE
> Task :core:classes UP-TO-DATE
> Task :core:jar
> Task :api:compileKotlin UP-TO-DATE
> Task :api:compileJava NO-SOURCE
> Task :api:classes UP-TO-DATE
> Task :api:jar UP-TO-DATE
> Task :web:compileKotlin UP-TO-DATE
> Task :web:compileJava NO-SOURCE
> Task :web:classes UP-TO-DATE
> Task :web:jar UP-TO-DATE
> Task :compileKotlin
> Task :compileJava NO-SOURCE
> Task :classes
> Task :resolveMainClassName

> Task :processAot
2023-11-18T17:03:52.661+08:00  INFO 3088 --- [           main] com.example.demo.DemoApplicationKt       : Starting DemoApplicationKt using Java 17.0.9 with PID 3088 (C:\Users\ju\IdeaProjects\demo\build\classes\kotlin\main started by ju in C:\Users\ju\IdeaProjects\demo)
2023-11-18T17:03:52.668+08:00  INFO 3088 --- [           main] com.example.demo.DemoApplicationKt       : No active profile set, falling back to 1 default profile: "default"

> Task :compileAotKotlin NO-SOURCE
> Task :compileAotJava UP-TO-DATE
> Task :processAotResources
> Task :aotClasses
> Task :jar

> Task :generateResourcesConfigFile
[native-image-plugin] Resources configuration written into C:\Users\ju\IdeaProjects\demo\build\native\generated\generateResourcesConfigFile\resource-config.json

> Task :nativeCompile
[native-image-plugin] GraalVM Toolchain detection is disabled
[native-image-plugin] GraalVM location read from environment variable: JAVA_HOME
[native-image-plugin] Native Image executable path: C:\Users\ju\.jdks\graalvm-jdk-17.0.9\bin\native-image.cmd
========================================================================================================================
GraalVM Native Image: Generating 'demo' (executable)...
========================================================================================================================
For detailed information and explanations on the build output, visit:
https://github.com/oracle/graal/blob/master/docs/reference-manual/native-image/BuildOutput.md
------------------------------------------------------------------------------------------------------------------------
[1/8] Initializing...                                                                                   (11.1s @ 0.15GB)
 Java version: 17.0.9+9, vendor version: GraalVM CE 17.0.9+9.1
 Graal compiler: optimization level: 2, target machine: x86-64-v3
 C compiler: cl.exe (microsoft, x64, 19.38.33130)
 Garbage collector: Serial GC (max heap size: 80% of RAM)
 1 user-specific feature(s)
 - org.springframework.aot.nativex.feature.PreComputeFieldFeature
Field org.springframework.core.NativeDetector#inNativeImage set to true at build time
Field org.springframework.boot.logging.logback.LogbackLoggingSystem$Factory#PRESENT set to true at build time
Field org.springframework.http.converter.json.Jackson2ObjectMapperBuilder#jackson2XmlPresent set to false at build time
Field org.springframework.boot.logging.java.JavaLoggingSystem$Factory#PRESENT set to true at build time
Field org.apache.commons.logging.LogAdapter#log4jSpiPresent set to true at build time
Field org.apache.commons.logging.LogAdapter#log4jSlf4jProviderPresent set to true at build time
Field org.apache.commons.logging.LogAdapter#slf4jSpiPresent set to true at build time
Field org.apache.commons.logging.LogAdapter#slf4jApiPresent set to true at build time
Field org.springframework.boot.logging.log4j2.Log4J2LoggingSystem$Factory#PRESENT set to false at build time
Field org.springframework.web.client.RestTemplate#romePresent set to false at build time
Field org.springframework.web.client.RestTemplate#jaxb2Present set to false at build time
Field org.springframework.web.client.RestTemplate#jackson2Present set to true at build time
Field org.springframework.web.client.RestTemplate#jackson2XmlPresent set to false at build time
Field org.springframework.web.client.RestTemplate#jackson2SmilePresent set to false at build time
Field org.springframework.web.client.RestTemplate#jackson2CborPresent set to false at build time
Field org.springframework.web.client.RestTemplate#gsonPresent set to false at build time
Field org.springframework.web.client.RestTemplate#jsonbPresent set to false at build time
Field org.springframework.web.client.RestTemplate#kotlinSerializationCborPresent set to false at build time
Field org.springframework.web.client.RestTemplate#kotlinSerializationJsonPresent set to false at build time
Field org.springframework.web.client.RestTemplate#kotlinSerializationProtobufPresent set to false at build time
Field org.springframework.core.KotlinDetector#kotlinPresent set to true at build time
Field org.springframework.core.KotlinDetector#kotlinReflectPresent set to true at build time
Field org.springframework.aot.AotDetector#inNativeImage set to true at build time
Field org.springframework.format.support.DefaultFormattingConversionService#jsr354Present set to false at build time
Field org.springframework.web.context.support.StandardServletEnvironment#jndiPresent set to true at build time
Field org.springframework.web.context.support.WebApplicationContextUtils#jsfPresent set to false at build time
Field org.springframework.cglib.core.AbstractClassGenerator#inNativeImage set to true at build time
Field org.springframework.web.context.request.RequestContextHolder#jsfPresent set to false at build time
Field org.springframework.boot.logging.logback.LogbackLoggingSystemProperties#JBOSS_LOGGING_PRESENT set to false at build time
Field org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport#romePresent set to false at build time
Field org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport#jaxb2Present set to false at build time
Field org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport#jackson2Present set to true at build time
Field org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport#jackson2XmlPresent set to false at build time
Field org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport#jackson2SmilePresent set to false at build time
Field org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport#jackson2CborPresent set to false at build time
Field org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport#gsonPresent set to false at build time
Field org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport#jsonbPresent set to false at build time
Field org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport#kotlinSerializationCborPresent set to false at build time
Field org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport#kotlinSerializationJsonPresent set to false at build time
Field org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport#kotlinSerializationProtobufPresent set to false at build time
Field org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter#jaxb2Present set to false at build time
Field org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter#jackson2Present set to true at build time
Field org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter#jackson2XmlPresent set to false at build time
Field org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter#jackson2SmilePresent set to false at build time
Field org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter#gsonPresent set to false at build time
Field org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter#jsonbPresent set to false at build time
Field org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter#kotlinSerializationCborPresent set to false at build time
Field org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter#kotlinSerializationJsonPresent set to false at build time
Field org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter#kotlinSerializationProtobufPresent set to false at build time
Field org.springframework.context.event.ApplicationListenerMethodAdapter#reactiveStreamsPresent set to false at build time
[2/8] Performing analysis...  [******]                                                                  (69.7s @ 1.75GB)
  15,805 (90.13%) of 17,536 types reachable
  25,534 (67.23%) of 37,980 fields reachable
  76,653 (61.27%) of 125,111 methods reachable
   5,137 types,   256 fields, and 4,079 methods registered for reflection
      83 types,    78 fields, and    68 methods registered for JNI access
       5 native libraries: crypt32, ncrypt, psapi, version, winhttp
[3/8] Building universe...                                                                              (10.3s @ 2.39GB)
[4/8] Parsing methods...      [***]                                                                      (6.1s @ 2.07GB)
[5/8] Inlining methods...     [***]                                                                      (3.6s @ 1.55GB)
[6/8] Compiling methods...    [*******]                                                                 (50.4s @ 2.29GB)
[7/8] Layouting methods...    [***]                                                                      (6.5s @ 3.38GB)
[8/8] Creating image...       [***]                                                                      (7.9s @ 4.13GB)
  37.33MB (52.11%) for code area:    49,317 compilation units
  33.89MB (47.31%) for image heap:  376,136 objects and 187 resources
 427.73kB ( 0.58%) for other data
  71.64MB in total
------------------------------------------------------------------------------------------------------------------------
Top 10 origins of code area:                                Top 10 object types in image heap:
  13.21MB java.base                                            8.10MB byte[] for code metadata
   4.28MB kotlin-reflect-1.8.22.jar                            3.86MB java.lang.Class
   3.76MB java.xml                                             3.71MB java.lang.String
   2.06MB jackson-databind-2.15.3.jar                          3.63MB byte[] for java.lang.String
   2.01MB tomcat-embed-core-10.1.15.jar                        3.06MB byte[] for general heap data
   1.44MB spring-core-6.0.13.jar                               1.38MB byte[] for embedded resources
   1.39MB svm.jar (Native Image)                               1.33MB com.oracle.svm.core.hub.DynamicHubCompanion
   1.10MB spring-boot-3.1.5.jar                              980.14kB byte[] for reflection metadata
 899.03kB spring-beans-6.0.13.jar                            767.10kB java.lang.String[]
 734.22kB logback-core-1.4.11.jar                            561.13kB c.o.svm.core.hub.DynamicHub$ReflectionMetadata
   6.12MB for 67 more packages                                 5.89MB for 3324 more object types
------------------------------------------------------------------------------------------------------------------------
Recommendations:
 HEAP: Set max heap for improved and more predictable memory usage.
 CPU:  Enable more CPU features with '-march=native' for improved performance.
------------------------------------------------------------------------------------------------------------------------
                        9.7s (5.7% of total time) in 97 GCs | Peak RSS: 5.29GB | CPU load: 3.02
------------------------------------------------------------------------------------------------------------------------
Produced artifacts:
 C:\Users\ju\IdeaProjects\demo\build\native\nativeCompile\demo.exe (executable)
========================================================================================================================
Finished generating 'demo' in 2m 47s.
[native-image-plugin] Native Image written to: C:\Users\ju\IdeaProjects\demo\build\native\nativeCompile

> Task :nativeRun FAILED
2023-11-18T17:06:45.621+08:00  INFO 10432 --- [           main] com.example.demo.DemoApplicationKt       : Starting AOT-processed DemoApplicationKt using Java 17.0.9 with PID 10432 (C:\Users\ju\IdeaProjects\demo\build\native\nativeCompile\demo.exe started by ju in C:\Users\ju\IdeaProjects\demo)
2023-11-18T17:06:45.621+08:00  INFO 10432 --- [           main] com.example.demo.DemoApplicationKt       : No active profile set, falling back to 1 default profile: "default"
2023-11-18T17:06:45.658+08:00  INFO 10432 --- [           main] com.example.demo.DemoApplicationKt       : Started DemoApplicationKt in 0.052 seconds (process running for 0.058)

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v3.1.5)

2023-11-18T17:06:45.667+08:00  INFO 10432 --- [           main] com.example.demo.DemoApplicationKt       : No active profile set, falling back to 1 default profile: "default"
2023-11-18T17:06:45.668+08:00  WARN 10432 --- [           main] w.s.c.ServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server
2023-11-18T17:06:45.668+08:00 ERROR 10432 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Web application could not be started as there was no org.springframework.boot.web.servlet.server.ServletWebServerFactory bean defined in the context.

Action:

Check your application's dependencies for a supported servlet web server.
Check the configured web application type.

Expected Behavior: The web application should start successfully without any errors.

Additional Information:

  • Spring Boot version: [3.1.5]
  • Graalvm-jdk: [17.0.9]
  • Kotlin: [1.8.22]

Please let me know if you require any further information or if there are specific logs or code snippets that would be helpful in resolving this issue.

Comment From: klfou

Source Code

demo.zip

Comment From: wilkinsona

Thanks for the report and sample project. Unfortunately, application context hierarchies are not supported by AOT processing or in a native image. We'd neglected to documented this on the wiki so I have just updated the general section to note this limitation.