简体   繁体   English

Visual Studio Code - Java - 导入错误等

[英]Visual Studio Code - Java - Import Errors and More

Just starting working on an existing project at work and wanted to use Visual Studio Code as my IDE (I have used it for a recent Rails project and loved it, so wanted to try with Java).刚刚开始在工作中处理现有项目,并想使用 Visual Studio Code 作为我的 IDE(我已将它用于最近的 Rails 项目并喜欢它,所以想尝试使用 Java)。

However, whenever I try to open one of the projects I receive a ton of different errors including:但是,每当我尝试打开其中一个项目时,都会收到大量不同的错误,包括:

  • When importing java.io, java.util, or anything similar, the error:导入 java.io、java.util 或任何类似内容时,出现以下错误:

"The import of java.io (or java.util) cannot be resolved" “无法解析java.io(或java.util)的导入”

  • Existing classes have an error:现有类有一个错误:

"The implicit super constructor is undefined for default constructor. Must define explicit constructor" “默认构造函数未定义隐式超级构造函数。必须定义显式构造函数”

  • Other random "cannot resolve to a type" errors.其他随机“无法解析为类型”错误。

All of these seem to stem from some sort of setting error I have with VS Code but can seem to find what it is.所有这些似乎都源于我对 VS Code 的某种设置错误,但似乎可以找到它是什么。 I have already uninstalled and reinstalled the RedHat plug-in that enables the Java language for VS Code.我已经卸载并重新安装了为 VS Code 启用 Java 语言的 RedHat 插件。

I have tried setting the java_home setting in the extension to the direct location of the install but that didn't work.我尝试将扩展中的 java_home 设置设置为安装的直接位置,但这没有用。 Tried uninstalling and reinstalling java and that also didn't work.尝试卸载并重新安装java,但也没有用。

My operating specs are as follows:我的操作规范如下:

  • OS: macOS Sierra操作系统:macOS Sierra

  • VS Code version 1.15.1 VS 代码版本 1.15.1

  • JDK version 1.8.0.144 JDK 版本 1.8.0.144

Any idea on what may be causing this?关于可能导致这种情况的任何想法? Do you think it may be an error in how I have VS Code set up or if it's an error (or rather incompatibility) with how the existing project is set up?您认为这可能是我设置 VS Code 的方式有误,还是与现有项目的设置方式有误(或不兼容)?

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/ .解决方案是从 VS Code 的工作区存储目录中删除所有内容,该目录位于$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我在这里找到了这个解决方案: 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.更新:从 Red Hat 0.33.0 版对 Java(TM) 的语言支持开始,现在可以在 VS Code 中完成此操作。 Open the command palette and type "java clean" (see official description in link).打开命令面板并键入“java clean”(请参阅​​链接中的官方说明)。

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.前面已经提到,需要清理项目,但是这有点困难,因为每个文件夹都是一个Guid,不知道清理哪个,因此需要删除所有内容。 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 .从插件的0.33.0版本开始,您也可以在 IDE 中自动执行此操作,使用CTRL + Shift + P并键入java clean ,IDE 将向您显示Java:清理 Java 语言服务器的建议提示工作区 Upon selection, agree and restart the IDE.选择后,同意并重新启动 IDE。 It will clean the language server workspace for you.它将为您清理语言服务器工作区。

Another approach can be, the Maven tools within the IDE.另一种方法可以是 IDE 中的 Maven 工具。 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,如果你安装了这个插件,你可以使用侧边栏并利用 Maven 项目助手选项来执行诸如清理、安装和打包等操作。例如,这是我拥有的项目和显示的选项,

在此处输入图片说明

That can be used, graphically, to manage your Maven-based projects.这可以以图形方式用于管理基于 Maven 的项目。 Also, this would work with the Java Extension Pack , not sure yet as to how it would behave with other extensions.此外,这将与Java Extension Pack 一起使用,但不确定它与其他扩展的行为方式。

For me: CMD + Shift + P Then type "Java: Clean Java language Server Workspace"对我来说: CMD + Shift + P 然后输入“Java: Clean Java language Server Workspace”

Note: This will reload/restart vscode as well.注意:这也会重新加载/重启 vscode。

I faced this issue after creating a whole Java project in one computer and then trying to run it on another computer.在一台计算机上创建了一个完整的 Java 项目,然后尝试在另一台计算机上运行它后,我遇到了这个问题。

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).在完成其他答案中所说的所有事情之后,真正使 VS Code 编译的是在 VS Code 中打开每个项目 java 文件并保存它(一个简单的 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.我只是运行了一个空的 main(),主体被注释掉了,同时仍然在开始时保留我的本地包/导入命令 - 没有错误。 After commenting in again, the project compiled without errors.再次注释后,项目编译无误。 Perhaps this refreshing effect might also help in this context?也许这种令人耳目一新的效果在这种情况下也可能有所帮助?

我必须清理这个文件夹才能让它在 Windows 上工作

%APPDATA%\\code\\Local Storage

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

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