'How to avoid VS Code warning: "[myfile].java is a non-project file, only syntax errors are reported"
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].*;
- 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.
- How can I fix build failed, do you want to continue? in vscode
- Visual Studio Code - Java - Import Errors and More
--> Thus, I cleaned vscode's workspaceStorage (on Windows: C:\Users\USER\AppData\Roaming\Code\User\workspaceStorage
) and restarted without success.
Solution 1:[1]
I got the same warning simply because I had two Java (Maven) projects in the same vscode workspace. Once I moved projectA out of the workspace, the warning for projectB is gone.
WorkspaceRoot
? projectA
????projectB
My current solution is to have one Java (Maven) project for one workspace, i.e, one Maven project per vscode workspace.
My guess is that vscode treats all Java projects inside the same workspace as as one project and hence, the projects interfering with each other.
Solution 2:[2]
Try cleaning the Java language server workspace:
- CMD+SHIFT+P (CTRL+SHIFT+P ON WINDOWS) to show command palette
- Choose "Java: Clean the Java language server workspace"
- Restart and Delete
Just worked for me.
Solution 3:[3]
This is an answer for those who do not use Maven.
The whole problem came up from loading not the direct project folder, but the parent folder, though the projects had been developed in their direct project folders from the start.
FOLDER1 (parent) contained
- FolderA (direct project folder of java files)
- FolderB (direct project folder of java files)
I have written the projects separately. But one time I opened the FOLDER1 in VS Code instead. That seems to have merged the 2 projects to just one project. After this, I changed back to opening only the FolderA/B and got the warnings that are reported in the question.
Now that I have opened FOLDER1 again and made both FolderA/B projects run without warnings (perhaps you might just comment out everything without fixing anything, but that is untested), opening the isolated FolderA/B projects threw no warnings either. Seems as if VS Code makes opening the parent folder the start of a new project which interferes with the child projects.
And the reason why I had a warning was a code error inside the other project's folder in the end (not important, but I had forgotten to load the local package needed for "FolderA" project at the start of some java file).
Whatever error I had, the warning was confusing, as I was only working on project "FolderB" which had nothing to do with "FolderA" and which had no code issue. This led to the strange effect that I got the warning of the "FolderA" project also in my "FolderB" project, because VS Code considered both as one project.
Solution 4:[4]
I struggled with this for a long time but did not find a proper solution on the internet. I somehow managed to do it by following these steps: Here are the actions that I've performed:
- There are folders you see in your left pane. (or press crt+shift+e to open the left pane).
- Right-click on them one by one and press "Add folder to Java Source Path."
The one suggested in the solutions didn't solve my problem 100%. The problem with this extension occurred "Language Support for Java(TM) by Red Hat," which let our folder away from the source path.
Solution 5:[5]
I renamed the package which contains "[myfile].java is a non-project file, only syntax errors are reported" and it worked for me.
Solution 6:[6]
For me, removing the code folder in the C:\Users\yourHome\AppData\Roaming path solves the problem.
Solution 7:[7]
I have used the VScode editor first-time for Java and I faced this issue with my simple HelloWorld program.
I tried to clean the editor's workspace but the issue wasn't resolved.
Then, I created a folder named mypractice in the C:// drive, following that added the same in the VSCode workspace, and then created a java file in that newly created workspace.
When I run that file, it worked without any warnings or errors.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | bkir |
Solution 3 | |
Solution 4 | |
Solution 5 | pinckerman |
Solution 6 | |
Solution 7 | Uwe Allner |