VisualStudio 代码-Java-导入错误及更多

刚开始工作中的一个现有项目,想使用 Visual Studio Code 作为我的 IDE (我最近在一个 Rails 项目中使用过它,非常喜欢,所以想试试 Java)。

然而,每当我尝试打开其中一个项目时,我都会收到大量不同的错误,包括:

  • 当导入 java.io、 java.util 或其他类似的文件时,会出现以下错误:

“ java.io (或 java.util)的导入无法解析”

  • 现有类有一个错误:

“隐式超级构造函数没有为缺省构造函数定义。必须定义显式构造函数”

  • 其他随机“无法解析为类型”错误。

所有这些似乎源于某种设置错误,我与 VS 代码,但似乎可以找到它是什么。我已经卸载并重新安装了 RedHat 插件,该插件为 VS 代码启用了 Java 语言。

我试过将扩展中的 java _ home 设置设置为安装的直接位置,但是没有用。尝试卸载和重新安装 java,也没有工作。

我的操作规范如下:

  • 操作系统: macOS Sierra

  • VS 代码版本1.15.1

  • JDK 版本1.8.0.144

知道是什么引起的吗?你认为这可能是一个错误,在我如何 VS 代码设置或如果它是一个错误(或更确切地说,不兼容)与现有的项目是如何设置?

113085 次浏览

I ran into a similar issue. The solution was to remove everything from VS Code's workspace storage directory, which was located at $HOME/Library/Application Support/Code/User/workspaceStorage/.

I found this solution here: https://github.com/redhat-developer/vscode-java/wiki/Troubleshooting#clean-the-workspace-directory

Update: This can now be done from within VS Code as of Language Support for Java(TM) by Red Hat Version 0.33.0. Open the command palette and type "java clean" (see official description in link).

As already mentioned previously, you require to clean the project, but that is a bit difficult thing because every folder is a Guid, and you do not know which one to clear, thus requiring you to delete everything. Starting with 0.33.0 version of the plugin you can automatically do that from within the IDE as well, use CTRL + Shift + P and type, java clean, and IDE will show you the suggestion tip for, Java: Clean the Java language server workspace. Upon selection, agree and restart the IDE. It will clean the language server workspace for you.

Another approach can be, the Maven tools within the IDE. If you have this plugin installed, you can use the side bar and utilize the Maven project helper options to perform actions like, clean, install, and package etc. For example, here is the project I am having and the options this shows,

enter image description here

That can be used, graphically, to manage your Maven-based projects. Also, this would work with the Java Extension Pack, not sure yet as to how it would behave with other extensions.

I had to clean this folder to get it working on Windows

%APPDATA%\code\Local Storage

I faced this issue after creating a whole Java project in one computer and then trying to run it on another computer.

After doing everything said in the other answers, what really made VS Code compile was to open each single project java file in VS Code and save it (a simple Ctrl + S). Maybe there is a simpler way of doing it, but that is what worked for me and I hope this helps anyone stuck in this issue.

I found another simple trick at least to get rid of "cannot be resolved to a type" errors which were coming from older workspaces and wrong project files I guess?. I just ran an empty main(), with the body commented out, while still keeping my local package / import commands at the start - no errors. After commenting in again, the project compiled without errors. Perhaps this refreshing effect might also help in this context?

For me: CMD + Shift + P Then type "Java: Clean Java language Server Workspace"

Note: This will reload/restart vscode as well.

Update: This appears to not fix it anymore for me. In my case I am using a gradle project, and needed to set the rootProject.name in the settings.gradle to be the same as the folder name that the project is in.

Press ctrl+shift+p then search 'java clean' and click 'java: clean java language server workspace' then click restart IDE.