简体   繁体   English

VS Code & WSL2 - 将 Java 语言级别指定为 1.8

[英]VS Code & WSL2 - specify Java Language Level to 1.8

I'm using VS Code in WSL2 and have installed a bunch of Java Extensions as per this link - normal Java programs work well - able to Run & Debug from VS Code.我在 WSL2 中使用 VS Code,并按照此链接安装了一堆 Java 扩展 - 正常的 Java 程序运行良好 - 能够从 VS Code 运行和调试。

Although when I start using Java 8 specific features like Lambda, it starts complaining about the source level for Java虽然当我开始使用 Java 8 个特定功能时,如 Lambda,它开始抱怨 Java 的源代码级别Java 8 特性

I've the following environment variables configured for Java Home我为 Java Home 配置了以下环境变量配置 Java 开发

What property should be specified in Settings to determine the source level as 1.8 and not lower !应该在设置中指定什么属性来确定源级别为 1.8 而不是更低!

Edit: The bottom blue bar says the current Java Runtime is J2SE1.5 for some reason.编辑:底部的蓝色条表示当前 Java 运行时出于某种原因是 J2SE1.5。 See Attached见附件J2SE1.5

On clicking, it asks to add the following property: java.configuration.runtimes and so I've added the following in my WSL2 settings for VS Code - still no effect !单击时,它会要求添加以下属性: java.configuration.runtimes所以我在 VS Code 的 WSL2 设置中添加了以下内容 - 仍然没有效果!

"java.configuration.runtimes": [
{
  "name": "JavaSE-1.8",
  "path": "/usr/lib/jvm/zulu-8-amd64",
  "default": true
}

] ]

Got my answer from Gitter that it picks up the language level settings from build file of the project.Gitter得到我的回答,它从项目的构建文件中获取语言级别设置。 So I added a basic POM file with proper source & target config for maven-compiler-plugin and restarted IDE.因此,我为 maven-compiler-plugin 添加了一个带有正确源和目标配置的基本 POM 文件,并重新启动了 IDE。 Voila, it works !瞧,它有效!

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
    <source>${maven.compiler.source}</source>
    <target>${maven.compiler.target}</target>
</configuration>

I think if you use jdk 11 or later, you maybe find that no matter how you configure settings.json file, the language level never change.我想如果您使用 jdk 11 或更高版本,您可能会发现无论您如何配置settings.json文件,语言级别都不会改变。

The problem is eclipse's setttings.问题是eclipse的设置。 in ${WorkSpaceRoot}\.setting\org.eclipse.jdt.core.prefs , there are 3 settings: targetPlatform , compliance , source , need to change to 1.8 or later version.${WorkSpaceRoot}\.setting\org.eclipse.jdt.core.prefs中,有 3 个设置: targetPlatformcompliancesource ,需要更改为1.8或更高版本。

And in .classpath file:.classpath文件中:

<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">

you can see there's a JavaSE-1.8 , you can change it to other version(which should meets the eclipse settings).您可以看到有一个JavaSE-1.8 ,您可以将其更改为其他版本(应符合 eclipse 设置)。

Also there's a quick way to do this: vscode can search in the workspace to find the number(in your case is 1.5 ), and replace them.还有一种快速的方法可以做到这一点:vscode 可以在工作区中搜索以查找数字(在您的情况下是1.5 ),然后替换它们。

I use the spring framework with gradle, lombok plugin and skdman (aside gradle, for maven is as good as well, I recommend use'em, they are all great), but I believe my configurations can be useful for everyone, so here it goes... I use the spring framework with gradle, lombok plugin and skdman (aside gradle, for maven is as good as well, I recommend use'em, they are all great), but I believe my configurations can be useful for everyone, so here it去...

One thing I may add thought its I got strange errors using plugins with command line gradle wich was corrected when i start using the vscode plugins interfaces only, therefor i would recommend give it a try and use the plugins buttons, if you have not done it yet.我可能会添加一件事,认为我在使用带有命令行 gradle 的插件时遇到了奇怪的错误,当我开始仅使用 vscode 插件界面时,该错误已得到纠正,因此我建议尝试使用插件按钮,如果你还没有这样做的话然而。

At launch.json I have the following debug configurations:在 launch.json 我有以下调试配置:

{
  "name": "Spring Project ???",
  "type": "java",
  "request": "launch",
  "cwd": "${workspacefolder}",
  "console": "internalConsole",
  "mainClass": "br.com.meuProject.???",
  "projectName": "???",
  "args": []
}
  • Note that the cwd is like workspacefolder, but you can change it to other values like fileDirname, however in these cases it may be legal to also define the value of the variable "program"请注意,cwd 类似于工作区文件夹,但您可以将其更改为其他值,例如 fileDirname,但是在这些情况下,定义变量“program”的值可能是合法的

In settings.json I have the following environment settings:在 settings.json 我有以下环境设置:

"java.semanticHighlighting.enabled": true,
"java.configuration.checkProjectSettingsExclusions": false,
"java.home": "C:\\Users\\name\\.sdkman\\candidates\\java\\current",
"java.jdt.ls.vmargs": "-XX:+UseParallelGC -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -Dsun.zip.disableMemoryMapping=true -Xmx1G -Xms100m -javaagent:\"/home/user/.vscode/extensions/gabrielbb.vscode-lombok-1.0.1/server/lombok.jar\"",
  • Note "java.home" defines you're JAVA_HOME variable.注意“java.home”定义你是 JAVA_HOME 变量。
  • Note "java.jdt.ls.vmargs" defines the variables passed to command line whenever building java.注意“java.jdt.ls.vmargs”定义了在构建 java 时传递给命令行的变量。

Hope to be of some use.希望能有点用。 Cheers!干杯!

In my case I had to configure my java runtime which is using java 7. (view -> command palette -> configure java runtime).在我的例子中,我必须配置我的 java 运行时,它使用 java 7。(查看 -> 命令面板 -> 配置 java 运行时)。 Here change java version to java 8 or later.此处将 java 版本更改为 java 8 或更高版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM