Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gradle AOT processing tasks do not use project's Java toolchain #37717

Closed
oldshensheep opened this issue Oct 4, 2023 · 3 comments
Closed

Gradle AOT processing tasks do not use project's Java toolchain #37717

oldshensheep opened this issue Oct 4, 2023 · 3 comments
Assignees
Labels
type: bug A general bug
Milestone

Comments

@oldshensheep
Copy link

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:

spring-io/initializr#1187
#33365

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Oct 4, 2023
@wilkinsona wilkinsona changed the title processAot not using gradle toolchain. processAot not using gradle toolchain Oct 4, 2023
@wilkinsona wilkinsona changed the title processAot not using gradle toolchain Gradle AOT processing tasks do not use project's Java toolchain Oct 4, 2023
@wilkinsona wilkinsona added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Oct 4, 2023
@wilkinsona wilkinsona added this to the 3.0.x milestone Oct 4, 2023
@wilkinsona
Copy link
Member

wilkinsona commented Oct 4, 2023

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))
}

@oldshensheep
Copy link
Author

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

@larsgrefer
Copy link
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

4 participants