1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
[info] [gradle-server] Gradle Server started, listening on 63127
[info] Gradle client connected to server
[info] Java Home: C:\Users\zheng\.cursor\extensions\redhat.java-1.43.1-win32-x64\jre\21.0.7-win32-x86_64
[info] JVM Args: --add-opens,java.base/java.util=ALL-UNNAMED,--add-opens,java.base/java.lang=ALL-UNNAMED,--add-opens,java.base/java.lang.invoke=ALL-UNNAMED,--add-opens,java.base/java.util=ALL-UNNAMED,--add-opens,java.prefs/java.util.prefs=ALL-UNNAMED,--add-opens,java.prefs/java.util.prefs=ALL-UNNAMED,--add-opens,java.base/java.nio.charset=ALL-UNNAMED,--add-opens,java.base/java.net=ALL-UNNAMED,--add-opens,java.base/java.util.concurrent.atomic=ALL-UNNAMED,-Xmx3G,-Dfile.encoding=UTF-8,-Duser.country=CN,-Duser.language=zh,-Duser.variant
[info] Gradle User Home: D:\gradle
[info] Gradle Version: 7.5.1
[error] FAILURE: Build failed with an exception.

* What went wrong:
Could not open cp_init generic class cache for initialization script 'C:\Users\zheng\AppData\Local\Temp\9e4672d3aade77a0f70c4c8543b48b94cfaaf99d20f28b9ba9873c486bd2e2d2.gradle' (D:\gradle\caches\7.5.1\scripts\6hvk7ow3e6yllf2drn472dvw0).
> BUG! exception in phase 'semantic analysis' in source unit '_BuildScript_' Unsupported class file major version 65

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

CONFIGURE FAILED in 18s
[error] [gradle-server] The supplied build action failed with an exception.
[error] Error getting build for d:\pokemon-lottery: The supplied build action failed with an exception.
[info] Found 0 tasks
[info] Java Home: C:\Users\zheng\.cursor\extensions\redhat.java-1.43.1-win32-x64\jre\21.0.7-win32-x86_64
[info] JVM Args: --add-opens,java.base/java.util=ALL-UNNAMED,--add-opens,java.base/java.lang=ALL-UNNAMED,--add-opens,java.base/java.lang.invoke=ALL-UNNAMED,--add-opens,java.base/java.util=ALL-UNNAMED,--add-opens,java.prefs/java.util.prefs=ALL-UNNAMED,--add-opens,java.prefs/java.util.prefs=ALL-UNNAMED,--add-opens,java.base/java.nio.charset=ALL-UNNAMED,--add-opens,java.base/java.net=ALL-UNNAMED,--add-opens,java.base/java.util.concurrent.atomic=ALL-UNNAMED,-Xmx3G,-Dfile.encoding=UTF-8,-Duser.country=CN,-Duser.language=zh,-Duser.variant
[info] Gradle User Home: D:\gradle
[info] Gradle Version: 7.5.1
[error] FAILURE: Build failed with an exception.

* What went wrong:
Could not open cp_init generic class cache for initialization script 'C:\Users\zheng\AppData\Local\Temp\9e4672d3aade77a0f70c4c8543b48b94cfaaf99d20f28b9ba9873c486bd2e2d2.gradle' (D:\gradle\caches\7.5.1\scripts\6hvk7ow3e6yllf2drn472dvw0).
> BUG! exception in phase 'semantic analysis' in source unit '_BuildScript_' Unsupported class file major version 65

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

CONFIGURE FAILED in 227ms
[error] [gradle-server] The supplied build action failed with an exception.
[error] Error getting build for d:\pokemon-lottery: The supplied build action failed with an exception.

截止将开发环境从idea适应到vscode以来,一直使用maven进行依赖管理。今天迫不得已需要维护一个使用gradle的项目

在vs中安装了相关的插件之后,打开项目始终无法构建。

可以看到开头明确了使用 redhat.java 下载的jdk21进行构建,而当前gradle版本最高只支持到dk17

不论是更改 JAVA_HOME 指向,还是更改vs设置中的java.home都无效

最后,在 https://github.com/redhat-developer/vscode-java/issues/3136 这个issue下找到答案

1
2
3
4
5
6
7
8
The exact Precedence is below.

settings.json java.import.gradle.java.home
settings.json java.jdt.ls.java.home (Default is Red Hat embedded JRE)
settings.json java.home (deprecated)
env var JAVA_HOME
env var PATH
The design problem is that the Gradle extension references java.jdt.ls.java.home for the Red Hat extension language server. This is counter-intuitive.

gradle扩展使用了 java.jdt.ls.java.home作为jdk服务,但我测试下,即使修改了这个值,gradle依然使用嵌入的jdk构建
最后的解决方案:
在vscode的setting.json中加入

1
"java.import.gradle.java.home": "C:\\lib\\store\\amazon_jdk1.8.0_265"

指定gradle使用的jdk即可,不过不同gradle版本所需版本不同。建议在工作区配置下修改

到此,问题解决