Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 342
questionto42standswithUkraine Avatar asked May 15 '20 19:05

questionto42standswithUkraine


People also ask

How do I run Java clean Java language workspace?

You can clean the workspace directory in the following way: Open the command palette (F1) select Java: Clean the Java Language Server Workspace. select Restart and delete from the confirmation prompt.

How do I create a Java package using Visual Studio code?

You can also create a Java project using the Java: Create Java Project command. Bring up the Command Palette (Ctrl+Shift+P) and then type java to search for this command. After selecting the command, you will be prompted for the location and name of the project. You can also choose your build tool from this command.

Is Vscode good for Java development?

Leveraging the power of Visual Studio Code, Java developers get an excellent tool for both quick code editing and also the full debugging and testing cycle. It's a great choice for your Java work if you're looking for a tool which: Is fast, lightweight, free, and open source.


2 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.

like image 65
bkir Avatar answered Nov 04 '22 03:11

bkir


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.

like image 37
kohane15 Avatar answered Nov 04 '22 02:11

kohane15