是否可以在 Visual Studio Code 中为 Java 项目选择 JDK 8?

ser*_*y_o 6 visual-studio-code

我正在尝试使用 Visual Studio Code 来编译和运行基于 Maven 的 Java 项目。

VSCODE 本身需要 JDK 11。我安装了 JDK 11 并将其添加为 VSCODE“Java:配置 Java 运行时”部分中的 java.home。

然而,该项目需要 Java 8。我有 JAVA_HOME 指向的 JDK 8,并且 pom.xml 指定

<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
Run Code Online (Sandbox Code Playgroud)

但是,当我编译并尝试在 VSCODE 中运行该项目时,我收到消息

Build path specifies execution environment JavaSE-1.8. There are no JREs installed in the workspace that are strictly compatible with this environment. 

The compiler compliance specified is 1.8 but a JRE 11 is used.
Run Code Online (Sandbox Code Playgroud)

即使 VSCODE 知道 JDK 8 是可以访问的:

在此输入图像描述

如何指示 VSCODE 使用 JDK 8(而不是 JDK 11)作为项目的运行时?

Tem*_*ayo 1

是的,这是可能的,因为 VSCode 支持 Java 1.5 及更高版本的项目。您需要关注运行时配置。检查下面的链接。 Java 运行时 VSCode

同时,我强烈建议您使用其他 IDE,例如 eclipse 或 Intellij for Java 项目,以进行正确的调试、透视、maven、gradle、库支持等