为什么 vscode 无法识别 import org.junit? [英] Why does vscode not recognize the import org.junit?

查看:76
本文介绍了为什么 vscode 无法识别 import org.junit?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 maven 在 Visual Studio 代码中开发一个 java 项目,目前正在尝试编写一个测试类.我已将 junit 作为依赖项添加到我的 pom.xml 文件中:

I am using maven to develop a java project within visual studio code, and am currently trying to write a test class. I have added junit as a dependency to my pom.xml file:

<dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
</dependency>

目前我的类是这样的:(Board类没有问题,getOne()返回1)

Currently, my class looks like this: (there are no problems with the Board class, and getOne() returns 1)

import org.junit.Test;

public class BoardTest {
    private Board board = new Board();

    @Test
    public void testOne() {
        assert(board.getOne() == 1);
    }
}

最初,当我打开文件时,一切都很好,但是一旦我保存,vscode 就会生成 2 条错误消息,当我关闭并重新打开文件时,这两条消息都消失了,但在保存后再次出现:

Initially, when I open the file, everything is fine, but as soon as I save, vscode generates 2 error messages, both of which go away when I close and reopen the file, but appear again after a save:

  1. 无法解析导入 org.junit"

  1. "The import org.junit cannot be resolved"

测试无法解析为类型"

有趣的是,即使存在这些错误,vscode 也会为我提供有关导入和 @Test 标志的鼠标悬停信息,就好像它实际上已经正确解决了它们一样.我已经从命令行运行 mvn install,vscode 甚至在项目的 java 依赖项部分列出了 junit-4.12.jar.

Interestingly, even with these errors present, vscode gives me mouseover information for both the import and the @Test flag, as if it has actually resolved them correctly. I have run mvn install from the command line, and vscode even lists junit-4.12.jar in the project's java dependencies section.

运行 mvn test 产生预期结果(测试通过),在 mvn package 之后,从命令行运行项目的 .jar 文件运行项目,没有问题.每当我尝试从 vscode 运行项目时,它都会通知我构建失败,即使当前不存在错误消息(即在我打开测试类之后但在我保存之前).如果我告诉 vscode 继续,该项目再次运行良好.尝试从 vscode 运行测试的工作方式相同(我收到一条错误消息,但在我告诉 vscode 继续进行后,测试照常通过).

Running mvn test yields the expected result (the test passes), and after mvn package, running the project's .jar file from the command line runs the project with no problems. Whenever I try to run the project from vscode, it gives me a notice that the build has failed, even if the error messages are not currently present (i.e. after I have opened the test class but before I have saved). If I tell vscode to proceed anyway, the project again runs fine. Trying to run the test from vscode works the same way (I get an error message, but the test passes as normal after I tell vscode to proceed anyway).

关于什么可能导致这种情况的任何想法?以下是我正在使用的所有内容的当前版本:

Any ideas as to what could cause this? Here are the current versions of everything that I am using:

JDK:openjdk v11.0.7

JDK: openjdk v11.0.7

vscode: v1.45.1

vscode: v1.45.1

maven:Apache Maven v3.6.3

maven: Apache Maven v3.6.3

推荐答案

试试这个 VS Code 命令...

Try this VS Code command...

View -> Command Palette -> Java: Clean Java Language Server Workspace

此命令解决了我的类似问题,即 VS Code 无法识别 Gradle 项目的 .java 文件中的 import org.json.*,即使 Gradle 可以成功构建和运行.这个 VS Code 命令类似于 IntelliJ 的 File ->使缓存无效并重新启动,因为它会强制刷新 IDE 的可用令牌和标识符缓存.

This command resolved my similar issue with VS Code not recognizing import org.json.* inside the .java files of a Gradle project even though Gradle would build and run successfully. This VS Code command is similar to IntelliJ's File -> Invalidate Caches and Restart in that it forces a refresh of the IDE's cache of available tokens and identifiers.

附注:

  • 确保至少运行一次您的构建工具(例如 gradle buildmvn install),以便将所需的包下载到您的本地计算机,以便 VS Code可以找到它们.
  • 此命令带有 VS Code 扩展Java 语言支持";来自 Red Hat (redhat.java),它捆绑在Java 扩展包"中来自 Microsoft (vscjava.vscode-java-pack).您必须安装这些扩展程序之一才能使用它.
  • Make sure to have run your build tool (e.g. gradle build or mvn install) at least once so that the required packages are downloaded to your local computer so VS Code can find them.
  • This command comes with the VS Code extension "Language Support for Java" from Red Hat (redhat.java) which comes bundled in the "Java Extension Pack" from Microsoft (vscjava.vscode-java-pack). You'll have to have one of these extensions installed to use it.

这篇关于为什么 vscode 无法识别 import org.junit?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆