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

Viewed 75567

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.

--> Thus, I cleaned vscode's workspaceStorage (on Windows: C:\Users\USER\AppData\Roaming\Code\User\workspaceStorage) and restarted without success.

10 Answers

Try cleaning the Java language server workspace:

  1. CMD+SHIFT+P (CTRL+SHIFT+P ON WINDOWS) to show command palette
  2. Choose "Java: Clean the Java language server workspace"
  3. Restart and Delete

Just worked for me.

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.

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.

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:

  1. There are folders you see in your left pane. (or press crt+shift+e to open the left pane).
  2. 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.

Today I ran into this problem while going off-script during a Intro to Java tutorial video and somehow fixed it. This solution may not work for everyone here but I hope it helps.

PROBLEMS (1): [myfile].java is a non-project file, only syntax errors are reported.

How I fixed it... Went to https://code.visualstudio.com/docs/languages/java and read sections "Working with Java source files" and "Working with Java projects". Basically when working with "Java projects" in VS Code, you must have the necessary extensions installed to work with those project files.

In my case, I needed to build a Maven project supported through the extension "Language Support for Java by Red Hat" and "Server Connector by Red Hat", since one of the extensions did not come with VS Code's "Coding Pack for Java - Windows" or the "Java Development Kit: Amazon Corretto".

Once I had my extensions downloaded I opened the Command Palette (Ctrl+Shift+P) and typed "Java:Create Java Project" > "Maven" > "Maven-Archetype-Quickstart" > "1.4" > [name] input group id > [name] input artifact id > then selected the folder I created that contained my Java file with the original error code (shown above). Once it was done processing, I reopened VS code and opened my folder. Ran the program and the error went away.

You may have to copy/move your files manually or press CTRL+Shift+E to open the left pane and add whatever you need to the Java Source Path.

Good Luck!

I renamed the package which contains "[myfile].java is a non-project file, only syntax errors are reported" and it worked for me.

For me, removing the code folder in the C:\Users\yourHome\AppData\Roaming path solves the problem.

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.

Instead of directly opening the Main.java file with vs code Don't do this

Open the folder in vs code by selecting the 'Open with code' option after right clicking it in the folder and then run the program so it wont show that problem again Do this

This is what helped me getting rid of that problem(Main.java is a non-project file, only syntax errors are reported) so I shared.

You should add your folder(in which your particular file is) to the workspace.

Related