Visual Studio Code - Java - 导入错误等
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45743779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Visual Studio Code - Java - Import Errors and More
提问by Reed Hermes
Just starting working on an existing project at work and wanted to use Visual Studio Code as my editor (I have used it for a recent Rails project and loved it so wanted to try it with Java).
刚开始在工作中处理现有项目,并想使用 Visual Studio Code 作为我的编辑器(我在最近的一个 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:
"The import of java.io (or java.util) cannot be resolved"
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.
导入 java.io、java.util 或任何类似内容时,出现以下错误:
“无法解析java.io(或java.util)的导入”
现有类有一个错误:
“默认构造函数未定义隐式超级构造函数。必须定义显式构造函数”
其他随机“无法解析为类型”错误。
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. I have already uninstalled and reinstalled the RedHat plug-in that enables the Java language for VS Code. I have tried setting the java_home setting in the extension to the direct location of the install but that didn't work. Tried uninstalling and reinstalling java and that also didn't work.
所有这些似乎都源于我对 VS Code 的某种设置错误,但似乎可以找到它是什么。我已经卸载并重新安装了为 VS Code 启用 Java 语言的 RedHat 插件。我尝试将扩展中的 java_home 设置设置为安装的直接位置,但这没有用。尝试卸载并重新安装java,但也没有用。
My operating specs are as follows
我的操作规格如下
OS: MacOS Sierra
VS Code version 1.15.1
JDK version 1.8.0.144
操作系统:MacOS Sierra
VS 代码版本 1.15.1
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 its an error (or rather incompatibility) with how the existing project have been setup? Thanks in advance.
关于可能导致这种情况的任何想法?您认为这可能是我设置 VS Code 的方式有误,还是与现有项目的设置方式有误(或不兼容)?提前致谢。
采纳答案by toreyhickman
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. Open the command palette and type "java clean" (see official description in link).
更新:从 Red Hat 0.33.0 版对 Java(TM) 的语言支持开始,现在可以在 VS Code 中完成此操作。打开命令面板并键入“java clean”(请参阅链接中的官方说明)。
回答by Afzaal Ahmad Zeeshan
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.0version of the plugin you can automatically do that from within the IDE as well, use CTRL+ Shift+ Pand 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.
前面已经提到,需要清理项目,但是这有点困难,因为每个文件夹都是一个Guid,不知道清理哪个,因此需要删除所有内容。与启动0.33.0版本的插件,你可以自动完成从IDE内为好,使用CTRL+ Shift+P和类型,java clean
以及IDE会显示你的建议小费,清洁Java语言服务器的工作区:Java的。选择后,同意并重新启动 IDE。它将为您清理语言服务器工作区。
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,
另一种方法可以是 IDE 中的 Maven 工具。如果你安装了这个插件,你可以使用侧边栏并利用 Maven 项目助手选项来执行诸如清理、安装和打包等操作。例如,这是我拥有的项目和显示的选项,
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.
这可以以图形方式用于管理基于 Maven 的项目。此外,这将与Java Extension Pack 一起使用,但不确定它与其他扩展的行为方式。
回答by bitsmonkey
I had to clean this folder to get it working on Windows
我必须清理这个文件夹才能让它在 Windows 上工作
%APPDATA%\code\Local Storage
%APPDATA%\code\Local Storage
回答by LucianoBAF
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). 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.
在完成其他答案中所说的所有事情之后,真正使 VS Code 编译的是在 VS Code 中打开每个项目 java 文件并保存它(一个简单的 Ctrl + S)。也许有一种更简单的方法可以做到这一点,但这对我有用,我希望这可以帮助任何陷入此问题的人。
回答by Lorenz
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?
我发现了另一个简单的技巧,至少可以摆脱“无法解析为类型”的错误,这些错误来自旧工作区和错误的项目文件,我猜?。我只是运行了一个空的 main(),主体被注释掉了,同时仍然在开始时保留我的本地包/导入命令 - 没有错误。再次注释后,项目编译无误。也许这种令人耳目一新的效果在这种情况下也可能有所帮助?