因为 vscode 无法识别 `import org.junit`。

huangapple go评论97阅读模式
英文:

Why does vscode not recognize the import org.junit?

问题

我正在使用Maven在Visual Studio Code中开发Java项目,目前正尝试编写一个测试类。
我已将JUnit添加为我的pom.xml文件的依赖:

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

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

import org.junit.Test;

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

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

最初,当我打开文件时,一切正常,但一旦保存,VS Code会生成两条错误消息,这两条错误消息在我关闭并重新打开文件后会消失,但在保存后会再次出现:

1. "The import org.junit cannot be resolved"
2. "Test cannot be resolved to a type"

有趣的是,即使出现这些错误,VS Code也会为导入和@Test标记提供鼠标悬停信息,就像它实际上已经正确解析了它们一样。
我已经从命令行运行了`mvn install`,而且VS Code甚至在项目的Java依赖项部分列出了junit-4.12.jar。

运行`mvn test`会产生预期结果(测试通过),而且在`mvn package`之后,从命令行运行项目的.jar文件也可以正常运行项目,没有任何问题。无论何时我尝试从VS Code运行项目,它都会通知我构建失败,即使错误消息当前不在(即在我打开测试类但尚未保存之前)。如果我告诉VS Code继续,项目再次正常运行。从VS Code运行测试也是同样的方式(我会收到错误消息,但在我告诉VS Code继续后,测试会像往常一样通过)。

有什么想法是什么原因可能会导致这种情况?以下是我正在使用的所有软件的当前版本:

JDK:openjdk v11.0.7
VS Code:v1.45.1
Maven:Apache Maven v3.6.3
英文:

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:

&lt;dependency&gt;
		&lt;groupId&gt;junit&lt;/groupId&gt;
		&lt;artifactId&gt;junit&lt;/artifactId&gt;
		&lt;version&gt;4.12&lt;/version&gt;
		&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;

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);
    }
}

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. "The import org.junit cannot be resolved"

  2. "Test cannot be resolved to a type"

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.

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

vscode: v1.45.1

maven: Apache Maven v3.6.3

答案1

得分: 25

尝试这个 VS Code 命令:

查看 -> 命令面板 -> Java: 清理 Java 语言服务器工作区

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

附注:

  • 确保至少运行了构建工具(例如 gradle buildmvn install)一次,以便所需的包被下载到本地计算机,这样 VS Code 才能找到它们。
  • 这个命令是通过 Red Hat 的 VS Code 扩展“Language Support for Java”(redhat.java)提供的,它包含在 Microsoft 的“Java 扩展包”(vscjava.vscode-java-pack)中。你需要安装这些扩展中的一个才能使用这个命令。
英文:

Try this VS Code command...

View -&gt; Command Palette -&gt; Java: Clean Java Language Server Workspace

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 -&gt; Invalidate Caches and Restart in that it forces a refresh of the IDE's cache of available tokens and identifiers.

Side Notes:

  • 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.

答案2

得分: 0

vscode 设置中设置 maven 用户的 settings.xml 路径,运行上述命令 View -> Command Palette -> Java: Clean Java Language Server Workspace,然后重新启动 vscode,这帮助我解决了这个问题。

英文:

Setting maven user settings.xml path in vscode settings, run the above command View -> Command Palette -> Java: Clean Java Language Server Workspace and restarting vscode helped me fix the issue.

huangapple
  • 本文由 发表于 2020年5月29日 06:16:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/62075442.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定