How to avoid VS Code warning: "[myfile].java is a non-project file, only syntax errors are reported"

que*_*o42 14 java project-structure package compiler-warnings visual-studio-code

I am running a build task in a java project in Visual Studio Code. The warning in the "PROBLEMS" tab:

[myfile].java is a non-project file, only syntax errors are reported

It refers to the first line where I load in the class file containing the main():

package [the project folder];
import [the project folder].[the file with other classes].*;
Run Code Online (Sandbox Code Playgroud)
  • I can only avoid the warning by copying the files' text (the code text itself) into new java files of a new project in a new unrelated folder. The code itself is correct and compiles without errors. Actually, this is the answer, but it is much manual work.
  • When I just copy the java files of the project with the warning message into a new folder, the warning still appears!!!! (!)
  • When I just copy the whole project folder to a new place, the error remains as well, of course.

I guess that copying text into new java files with the same names and the same folder structure is different from copying the files themselves because the files probably get tagged by VS Code, so that they have a project stamp even when the folder structure is destroyed. Perhaps this supports recovering the project structure from recovered raw files? Could this be the problem of this Visual Code warning?

I checked other threads before, this is just the last step.

--> 因此,我清理了 vscode 的 workspaceStorage(在 Windows 上:)C:\Users\USER\AppData\Roaming\Code\User\workspaceStorage并重新启动但没有成功。

koh*_*e15 9

我收到同样的警告只是因为我在同一个 vscode 工作区中有两个 Java (Maven) 项目。一旦我将 projectA 移出工作区,projectB 的警告就消失了。

WorkspaceRoot
?   projectA
????projectB
Run Code Online (Sandbox Code Playgroud)

我当前的解决方案是为一个工作区创建一个 Java (Maven) 项目,即每个 vscode 工作区一个 Maven 项目。

我的猜测是 vscode 将同一工作区中的所有 Java 项目视为一个项目,因此这些项目相互干扰。


小智 8

尝试清理 Java 语言服务器工作区:

  1. CMD+SHIFT+P (CTRL+SHIFT+P 在 WINDOWS 上) 显示命令面板
  2. 选择“Java:清理 Java 语言服务器工作区”
  3. 重启和删除

刚刚为我工作。

  • 欢迎来到 SO 并感谢您的贡献,但这已经被排除在问题本身中,它说:***“我清理了 vscode 的工作空间存储(在 Windows 上:C:\Users\USER\AppData\Roaming\Code\User\workspaceStorage )并重新启动,但没有成功。”*** 您已提供其他给定链接的答案。它可能仍然对迷失在链接中的人有所帮助,但这不是这里问题的答案。 (2认同)

que*_*o42 6

这是给那些不使用 Maven 的人的答案。

整个问题是由于加载的不是直接项目文件夹而是父文件夹而产生的,尽管这些项目从一开始就是在其直接项目文件夹中开发的。

包含 FOLDER1(父级)

  • FolderA(Java文件的直接项目文件夹)
  • FolderB(Java文件的直接项目文件夹)

我已经分别写了项目。但有一次我在 Visual Studio Code 中打开了 FOLDER1。这似乎已将两个项目合并为一个项目。此后,我改回仅打开文件夹 A/B 并收到问题中报告的警告。

现在,我再次打开 FOLDER1 并使两个 FolderA/B 项目在没有警告的情况下运行(也许您可能只是注释掉所有内容而不修复任何内容,但这未经测试),打开隔离的 FolderA/B 项目也没有发出任何警告。似乎 Visual Studio Code 使打开父文件夹成为新项目的开始,这会干扰子项目。

我收到警告的原因是最后另一个项目的文件夹内出现了代码错误(这并不重要,但我忘记在某些Java文件的开头加载“FolderA”项目所需的本地包)。

无论我遇到什么错误,警告都很令人困惑,因为我只在项目“FolderB”上工作,该项目与“FolderA”无关,并且没有代码问题。这导致了奇怪的效果,我在“FolderB”项目中也收到了“FolderA”项目的警告,因为 Visual Studio Code 将两者视为一个项目


小智 5

我为此苦恼了很长时间,但在互联网上没有找到合适的解决方案。我以某种方式设法通过以下步骤做到了这一点:

以下是我已执行的操作:

  1. 您可以在左侧窗格中看到一些文件夹。(或按Ctrl+ Shift+E打开左窗格)。
  2. 逐一右键单击它们,然后按“将文件夹添加到 Java 源路径”。

解决方案中建议的方案并没有100%解决我的问题。此扩展的问题发生在“Red Hat 对 Java(TM) 的语言支持”,这让我们的文件夹远离了源路径。