3

I'm on Windows, assuming I'm downloading a default demo project from Spring Initializr as follows:

Now for the project to run I'll also add default H2 support to the application.properties:

spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

Next, I'll add a simple HelloWorldController.java controller as follows:

package com.example.demo;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWorldController
{
    @GetMapping("/")
    public String index()
    {
        return "Greetings from Spring Boot!";
    }
}

I can run the REST API successfully from IntelliJ IDEA or compile an executable JAR and run that, going to http://localhost:8080 in the browser works as expected.

My native-image version is:

>where gu
C:\graalvm-ee-java19-windows-amd64-22.3.1\bin\gu.cmd

My Java version is:

>java -version
java version "19.0.1" 2022-10-18
Java(TM) SE Runtime Environment (build 19.0.1+10-21)
Java HotSpot(TM) 64-Bit Server VM (build 19.0.1+10-21, mixed mode, sharing)

According to the HELP.md I can compile a native image by running mvnw native:compile -Pnative (via an administrator x64 Native Tools Command Prompt for VS 2022) so I did. The first error I ran into was related to spaces in the file path (Exception in thread "main" java.nio.file.InvalidPathException: Illegal char <> at index 3), I fixed it by moving the whole project into a file path without spaces. (However, that shouldn't even be a problem these days.)

Now, after waiting for about 8 minutes, the native compilation was successful. Upon running the demo.exe, it failed to start:

D:\Moved\demo>target\demo

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

2023-04-26T00:04:22.787+02:00  INFO 28544 --- [           main] com.example.demo.DemoApplication         : Starting AOT-processed DemoApplication using Java 19.0.2 with PID 28544 (D:\Moved\demo\target\demo.exe started by BullyWiiPlaza in D:\Moved\demo)
2023-04-26T00:04:22.787+02:00  INFO 28544 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to 1 default profile: "default"
2023-04-26T00:04:23.017+02:00  INFO 28544 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2023-04-26T00:04:23.020+02:00  INFO 28544 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2023-04-26T00:04:23.020+02:00  INFO 28544 --- [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.8]
2023-04-26T00:04:23.052+02:00  INFO 28544 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2023-04-26T00:04:23.053+02:00  INFO 28544 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 265 ms
2023-04-26T00:04:23.107+02:00  INFO 28544 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2023-04-26T00:04:23.113+02:00  INFO 28544 --- [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Added connection conn0: url=jdbc:h2:mem:testdb user=SA
2023-04-26T00:04:23.114+02:00  INFO 28544 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2023-04-26T00:04:23.116+02:00  WARN 28544 --- [           main] w.s.c.ServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory': Instantiation of supplied bean failed
2023-04-26T00:04:23.116+02:00  INFO 28544 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2023-04-26T00:04:23.121+02:00  INFO 28544 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.
2023-04-26T00:04:23.123+02:00  INFO 28544 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2023-04-26T00:04:23.127+02:00 ERROR 28544 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory': Instantiation of supplied bean failed
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.obtainFromSupplier(AbstractAutowireCapableBeanFactory.java:1220) ~[demo.exe:6.0.8]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1158) ~[demo.exe:6.0.8]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:560) ~[demo.exe:6.0.8]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520) ~[demo.exe:6.0.8]
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) ~[demo.exe:6.0.8]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[demo.exe:6.0.8]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) ~[demo.exe:6.0.8]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[demo.exe:6.0.8]
        at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1132) ~[demo.exe:6.0.8]
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:907) ~[demo.exe:6.0.8]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:584) ~[demo.exe:6.0.8]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[demo.exe:3.0.6]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:732) ~[demo.exe:3.0.6]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:434) ~[demo.exe:3.0.6]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:310) ~[demo.exe:3.0.6]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1304) ~[demo.exe:3.0.6]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1293) ~[demo.exe:3.0.6]
        at com.example.demo.DemoApplication.main(DemoApplication.java:10) ~[demo.exe:na]
Caused by: java.lang.IllegalStateException: No available JtaPlatform candidates amongst [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform, org.hibernate.service.jta.platform.internal.NoJtaPlatform]
        at org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration.getNoJtaPlatformManager(HibernateJpaConfiguration.java:213) ~[demo.exe:na]
        at org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration.configureJtaPlatform(HibernateJpaConfiguration.java:150) ~[demo.exe:na]
        at org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration.customizeVendorProperties(HibernateJpaConfiguration.java:139) ~[demo.exe:na]
        at org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.entityManagerFactory(JpaBaseConfiguration.java:132) ~[demo.exe:na]
        at org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration__BeanDefinitions.lambda$getEntityManagerFactoryInstanceSupplier$3(JpaBaseConfiguration__BeanDefinitions.java:84) ~[na:na]
        at org.springframework.util.function.ThrowingBiFunction.apply(ThrowingBiFunction.java:68) ~[demo.exe:6.0.8]
        at org.springframework.util.function.ThrowingBiFunction.apply(ThrowingBiFunction.java:54) ~[demo.exe:6.0.8]
        at org.springframework.beans.factory.aot.BeanInstanceSupplier.lambda$get$2(BeanInstanceSupplier.java:204) ~[na:na]
        at org.springframework.util.function.ThrowingSupplier.get(ThrowingSupplier.java:58) ~[demo.exe:6.0.8]
        at org.springframework.util.function.ThrowingSupplier.get(ThrowingSupplier.java:46) ~[demo.exe:6.0.8]
        at org.springframework.beans.factory.aot.BeanInstanceSupplier.invokeBeanSupplier(BeanInstanceSupplier.java:216) ~[na:na]
        at org.springframework.beans.factory.aot.BeanInstanceSupplier.get(BeanInstanceSupplier.java:204) ~[na:na]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.obtainInstanceFromSupplier(DefaultListableBeanFactory.java:947) ~[demo.exe:6.0.8]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.obtainFromSupplier(AbstractAutowireCapableBeanFactory.java:1214) ~[demo.exe:6.0.8]
        ... 17 common frames omitted

So, it isn't working, why?

After researching more, I found this issue and related ones. Also, trying Spring Boot version 3.0.5 is working fine while 3.0.6 fails.

BullyWiiPlaza
  • 17,329
  • 10
  • 113
  • 185
  • And your question is? This seems more of a rant against graalvm then an actual question. – M. Deinum Apr 26 '23 at 12:49
  • The title looks like a pretty clear and well formed question @m-deinum – ch4mp Apr 26 '23 at 18:59
  • @xerx593: I used a separate `JDK19` installation, not the one by `GraalVM`. It seems like it doesn't cause any problems – BullyWiiPlaza Apr 26 '23 at 20:58
  • It does not cause any problem because, with `mvn -Pnative spring-boot:build-image`, the native image is built in a docker container which pulls a GraalVM image – ch4mp Apr 26 '23 at 23:14

1 Answers1

2

I faced the same regression due to missing native hints and found 3 easy workarounds in a thread linked at the bottom of the one you point to. Here are the options from the less invasive (prefered) to the most:

  • downgrade the metadata repository from 0.2.7 to 0.2.6 in native-maven-plugin version 0.9.21 (which is the default for Boot 3.0.6):
<plugin>
    <groupId>org.graalvm.buildtools</groupId>
    <artifactId>native-maven-plugin</artifactId>
    <configuration>
        <metadataRepository>
            <enabled>true</enabled>
            <version>0.2.6</version>
        </metadataRepository>
    </configuration>
</plugin>
  • use an earlier metadata repository for hibernate-core (6.1.1.Final instead of 6.1.7.Final which is the default for Boot 3.0.6):
<plugin>
    <groupId>org.graalvm.buildtools</groupId>
    <artifactId>native-maven-plugin</artifactId>
    <configuration>
        <metadataRepository>
            <dependencies>
                <dependency>
                    <groupId>org.hibernate.orm</groupId>
                    <artifactId>hibernate-core</artifactId>
                    <metadataVersion>6.1.1.Final</metadataVersion>
                </dependency>
            </dependencies>
        </metadataRepository>
    </configuration>
</plugin>
  • downgrade native-maven-plugin from 0.9.21 to 0.9.20:
<plugin>
    <groupId>org.graalvm.buildtools</groupId>
    <artifactId>native-maven-plugin</artifactId>
    <version>0.9.20</version>
</plugin>

The issue is tracked here for Spring Boot and there for native-build-tools

P.S.

For those working with Gradle rather than maven, there is an equivalent of the first workaround in the Spring Boot thread

graalvmNative {
    metadataRepository {
        version = "0.2.6"
    }
}
ch4mp
  • 6,622
  • 6
  • 29
  • 49