简体   繁体   English

在 Visual Studio Code (VsCode) 和 Java 中使用 SonarLint 时出错 11

[英]Error using SonarLint in Visual Studio Code (VsCode) and Java 11

I have a problem using SonarLint in visual studio code, I've been using very well this plugin in java 8 but arround last week, Vscode required java 11 as minimum java version and when I updated this (I keep using java 8 for my project, according to Vscode can use two versions), the plugin couldn't start well, this is a error: [Error - 16:57:02.682] Analysis failed.我在 visual studio code 中使用 SonarLint 时遇到问题,我在 java 8 中使用这个插件非常好,但上周左右,Vscode 需要 java 11 作为最低 java 版本,当我更新这个时(我继续使用 java 8 为我的项目,根据Vscode可以使用两个版本),插件无法正常启动,这是一个错误:[Error - 16:57:02.682] Analysis failed。

[Error - 16:57:02.683] java.lang.IllegalStateException: No files nor directories matching 'C:\Users\MyUser.m2\repository\com\sun\java\tools\11\tools-11.jar' [错误 - 16:57:02.683] java.lang.IllegalStateException:没有文件或目录匹配 'C:\Users\MyUser.m2\repository\com\sun\java\tools\11\tools-11.jar'

I know that tools.jar doesn't exist in java 11 and I couldn't find a jar to replace it or a way to solve this.我知道 tools.jar 在 java 11 中不存在,我找不到 jar 来替换它或解决这个问题的方法。 I'm using the last version of sonarLint plugin 1.17.0我正在使用最新版本的 sonarLint 插件 1.17.0

Thanks for your answers.感谢您的回答。

Requirements The SonarLint language server needs a Java Runtime (JRE) 8 or 11. If one is already installed on your computer, SonarLint should automatically find and use it.要求 SonarLint 语言服务器需要一个 Java Runtime (JRE) 8 或 11。如果您的计算机上已经安装了一个,SonarLint 应该会自动找到并使用它。

If a suitable JRE cannot be found at the usual places, SonarLint will ask for your permission to download and manage its own version.如果在通常的地方找不到合适的 JRE,SonarLint 会征求您的许可以下载和管理自己的版本。

Finally, you can explicitly set the path where the JRE is installed using the sonarlint.ls.javaHome variable in VS Code settings.最后,您可以使用 VS Code 设置中的 sonarlint.ls.javaHome 变量显式设置 JRE 的安装路径。 For instance:例如:

{
    "sonarlint.ls.javaHome": "C:\\Program Files\\Java\\jre-11.0.11"
}

On Windows, backslashes must be escaped, eg C:\Program Files\Java\jdk-11.0.11 On macOS, this path should include the /Contents/Home directory, eg /Library/Java/JavaVirtualMachines/jdk-11.0.11.jdk/Contents/Home在 Windows 上,必须转义反斜杠,例如 C:\Program Files\Java\jdk-11.0.11 在 macOS 上,此路径应包括 /Contents/Home 目录,例如 /Library/Java/Java.0.1Machines/jdk- jdk/目录/首页

To analyze JavaScript and TypeScript, SonarLint will also need Node.js.要分析 JavaScript 和 TypeScript,SonarLint 还需要 Node.js。

To enable the support for Java, you need the Language support for Java VSCode extension (version 0.56.0 or higher).要启用对 Java 的支持,您需要对 Java VSCode 扩展(版本 0.56.0 或更高版本)的语言支持。

The support for Apex and PL/SQL is only available together with SonarQube/SonarCloud.对 Apex 和 PL/SQL 的支持仅与 SonarQube/SonarCloud 一起提供。 For Apex, you'll also need the Salesforce Extension Pack VSCode extension.对于 Apex,您还需要 Salesforce 扩展包 VSCode 扩展。

Just a supplement to the answer, if you are using VSCode in Windows with WSL and installed the "Remote - WSL" extension.如果您在 Windows 中将 VSCode 与 WSL 一起使用并安装了“Remote - WSL”扩展,则只是对答案的补充。 You need to:你需要:

  1. Install the JDK 11 in the WSL (eg Ubuntu)在 WSL(例如 Ubuntu)中安装 JDK 11
  2. Update the settings.json under WSL /home/$username/.vscode-server/data/Machine to add the attriutte "sonarlint.ls.javaHome" and set the value to the home path of the JDK 11 in the WSL更新WSL /home/$username/.vscode-server/data/Machine下的settings.json,添加属性“sonarlint.ls.javaHome”,并将值设置为JDK 11在WSL中的home路径

I tried it, and everything works well.我试过了,一切正常。 Could I verify somethings?我可以验证一些东西吗?

Do you set the settings.json file like this?你是这样设置settings.json文件的吗?

"java.home": "[Path to JDK]", // For example: D:\\workSoft\\JDK11

"java.configuration.runtimes": [  // This should be setted in User settings.json
    {
      "name": "JavaSE-11",
      "path": "[Path to JDK]"
    },
    {
      "name": "JavaSE-1.8",
      "path": "[Path to JDK]",
      "default": true
    }
  ]

I tried this, and everything works well.我试过了,一切正常。 The java extension depends on JDK11 and my project depends on JDK8. java扩展依赖JDK11,我的项目依赖JDK8。 SonarLint works well too. SonarLint 也很好用。

And you can find this in the details page of SonarLint extension:您可以在 SonarLint 扩展的详细信息页面中找到它:

The SonarLint language server needs a Java Runtime (JRE) 8 or 11 . SonarLint 语言服务器需要 Java Runtime (JRE) 8 或 11 If one is already installed on your computer, SonarLint should automatically find and use it.如果您的计算机上已经安装了一个,SonarLint 应该会自动找到并使用它。 If a suitable JRE cannot be found at the usual places, SonarLint will ask for your permission to download and manage its own version.如果在通常的地方找不到合适的 JRE,SonarLint 会征求您的许可以下载和管理自己的版本。 Finally, you can explicitly set the path where the JRE is installed using the sonarlint.ls.javaHome variable in VS Code settings.最后,您可以使用 VS Code 设置中的 sonarlint.ls.javaHome 变量显式设置 JRE 的安装路径。 For instance:例如:

{
    "sonarlint.ls.javaHome": "C:\\Program Files\\Java\\jre1.8.0_131"
    //I tried: both of "D:\\workSoft\\JDK8" and "D:\\workSoft\\JDK11" works.
}

Go to File-> Preferences -> Settings Go 到文件-> 首选项-> 设置

Then search for sonarlint to bring up its settings然后搜索 sonarlint 以调出其设置

Copy-paste your java directory path here.在此处复制粘贴您的 java 目录路径。 In my case, I had to escape the backslashes.就我而言,我必须避开反斜杠。

在此处输入图像描述

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

相关问题 SonarLint 在 VSCode 上缺少 Java 规则 - SonarLint missing Java Rule on VSCode Visual Studio Code Java 扩展包起始模板 (vscode) - Visual Studio Code Java Extension Pack starting template (vscode) VSCode SonarQube 支持 Visual Studio Code 错误 400 - VSCode SonarQube support for Visual Studio Code Error 400 Java Flutter 中的错误 - Visual Studio 代码 - Java Error in Flutter - Visual Studio Code 在Visual Studio代码编译错误中调试Java - Debugging Java in Visual Studio Code Compilation Error Visual Studio 代码中的 Java - Java in Visual Studio Code Visual Studio Code 显示“需要 Java 11 或更高版本才能运行。请下载并安装最新的 JDK” - Visual Studio Code showing "Java 11 or more recent is required to run. Please download and install a recent JDK" 如何在 Ubuntu 21.04 上使用 OpenJDK 11 在 Visual Studio Code 中查看 Java 标准库文档弹出窗口? - How to view Java standard library documentation popup in Visual Studio Code with OpenJDK 11 on Ubuntu 21.04? 获取错误文件无法解析为 Visual Studio 代码中 java 的类型 - Getting error File cannot be resolved to a type for java in visual studio code 在 Visual Studio Code 中获得与 PowerShell 或 CMD 中相同的 JAVA 编译错误 - Get same JAVA compilation error as in PowerShell or CMD in Visual Studio Code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM