The current processAot task does not utilize the gradle toolchain, and it will fail if the Java environment version is lower than the toolchain language version. In my project settings, I have defined the Java language version as 21 using the following configuration:

java {
    toolchain {
        languageVersion.set(JavaLanguageVersion.of(21))
    }
}

However, my Java environment is using Java 17. When I run gradle build, it fails with the following error:

Task :processAot FAILED
Exception in thread "main" java.lang.UnsupportedClassVersionError: com/oldshensheep/Main has been compiled by a more recent version of the Java Runtime (class file version 65.0), this version of the Java Runtime only recognizes class file versions up to 61.0
    at java.base/java.lang.ClassLoader.defineClass1(Native Method)
    at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1017)
    at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:150)
    at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:862)
    at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:760)
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:681)
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:639)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:525)
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:375)
    at org.springframework.boot.SpringApplicationAotProcessor.main(SpringApplicationAotProcessor.java:70)

Execution failed for task ':processAot'.
> Process 'command 'E:\apps\scoop\apps\temurin-lts-jdk\current\bin\java.exe'' finished with non-zero exit value 1

I've found similar issues, but it is not the case:

https://github.com/spring-io/initializr/issues/1187 https://github.com/spring-projects/spring-boot/issues/33365

Comment From: wilkinsona

Thanks for the report. We do this already for bootRun and the new bootTestRun task but overlooked it when introducing processAot and processTestAot.

Until a fix is released, you can work around the problem by configuring the toolchain in your build. Something like this:

tasks.withType(JavaExec).configureEach {
    javaLauncher.set(javaToolchains.launcherFor(java.toolchain))
}

Comment From: oldshensheep

@wilkinsona It's working, but I need to replace JavaExec with JavaExec::class.

Comment From: larsgrefer

I used this similar workaround:

https://github.com/joinfaces/joinfaces/blob/91aac91b7cd1de63e25fae1f477a6eb6604009b4/test-projects/build.gradle#L37-L42