Spring Boot and Gradle on Java 21

by

hey, while Gradle doesn't officially support Java 21, you can still build and work with Spring Boot on Java 21.

Here are the steps:

  • install GraalVM and Java 21: sdk install 21-graalce
  • create a new project on the Spring Initializr
  • open it in your IDE. I did: idea build.gradle. Make sure to point your IDE JDK to Java 21.
  • add the following method to your DemoApplication.java
@Bean
ApplicationRunner runner () {
 return args -> Executors
   .newVirtualThreadPerTaskExecutor()
   .submit(() -> System.out.println("hello, world!"));
}
  • ./gradlew bootRun // it's running Java 21 code in Gradle
  • ./gradlew bootJar && java -jar ./build/libs/demo-0.0.1-SNAPSHOT.jar // it's produced a binary with Java 21

More posts

Comments powered by Talkyard.