24

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.

7 Answers7

22

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.

bkir
  • 221
  • 2
  • 2
  • Welcome to SO and thank you for the contribution, yet this is already excluded in the question itself which says: ***"I cleaned vscode's workspaceStorage (on Windows: C:\Users\USER\AppData\Roaming\Code\User\workspaceStorage) and restarted without success."*** You have provided the answer of the other given links. It might still help someone, who is lost in the links, but it is not the answer to the question here. – questionto42standswithUkraine Nov 05 '20 at 07:56
  • 1
    Why do you say "3. restart and delete"? I would have expected it to be just "restart". – questionto42standswithUkraine Nov 05 '20 at 07:58
  • Sorry, "Restart and Delete" is the prompt/popup when you choose "Java: Clean the Java language server workspace". Think it deletes the cache and restarts in one action. I'm not sure about cleaning the folder directly as you mentioned... Did you execute the VSCode command from the command palette? – bkir Nov 06 '20 at 18:15
  • I did the same as you, inside vscode, going to the preferences, cleaning the workspace. That did not help. And cleaning the workspace manually did not help either. Though it seems so weird at first, the error was reproducible. – questionto42standswithUkraine Nov 06 '20 at 20:14
  • In the version 1.65 it is *Java: Clean Java language server workspace* – Ravindra Gullapalli Mar 10 '22 at 07:51
11

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.

kohane15
  • 643
  • 11
  • 14
  • You are the first to delete just a part of the workspace, brilliant idea. I accept it instead of my suggestion. I might have done the same, after solving the errors in both projects, with re-opening a single project in a new workspace (I was not in the workspace folder for this). I did not know for sure the reason for the solution. If you do not mind, put a screenshot and / or the path to the workspace with slightly more details, or an SO link how this is done, so that everyone can quickly solve the issue as well. Relevant links to find the workspace are at the bottom of the question. – questionto42standswithUkraine Jul 31 '20 at 12:12
  • Just to make sure we are talking about the same workspace. Do you mean the **workspace directory** like at 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 or the **working directory** like you find it in your project folder? – questionto42standswithUkraine Aug 07 '20 at 16:26
  • 1
    @Lorenz I'm not sure with the terminology. In vscode explorer you can "Add Folder to Workspace". If inside that folder you have two maven projects then it's likely to interfere with each other. The likely solution is to import one maven project per folder (into the same workspace is fine). – kohane15 Aug 08 '20 at 05:28
4

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.

2

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.

1

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

pinckerman
  • 3,999
  • 6
  • 30
  • 41
Thuan Tran
  • 111
  • 7
0

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

hamza
  • 1
  • 1
  • For some, it might be the solution, since it is the solution of the highly frequented links mentioned at the bottom of the question. In my case, it could not solve the issue. – questionto42standswithUkraine Sep 11 '20 at 13:08
  • Do u want to build a maven project? did u literally remove the code folder? because I've never seen like this solution before. for me I VScode couldn't run the spring boot application.java but the mvn spring-boot:run command works, and I had another laptop has the VScode 1.43 version works fine, so all I had to do is to remove my VScode totally including the .VScode and code folders and install VScode 1.43 version! and it works – hamza Sep 12 '20 at 15:49
  • I do not want to build a Maven project. No I did not remove the whole `Code` folder. I thought you still meant the `C:\Users\USER\AppData\Roaming\Code\User\workspaceStorage`. Then your answer is new, I have not seen it anywhere else. I cannot test your answer, let us wait for upvotes of your answer to see whether it works. As for me, I do not want to downgrade vscode to solve that error, there should be a better way, and deleting the whole `Code` folder seems one step too far for me. I think you rather just cleaned your workspaceStorage by that. Which is an already known option. – questionto42standswithUkraine Sep 14 '20 at 10:45
  • I see, downgrade my VSCode was just because I still use Java 8, new VSCode ask for java 11. I think removing the code folder solves the problem if you've nothing to lose! – hamza Sep 14 '20 at 14:09
  • Your workaround can also be reached without downgrading vscode, see https://stackoverflow.com/questions/63043585/visual-studio-code-showing-java-11-or-more-recent-is-required-to-run-please-do/63287737#63287737. Or you install Java 11 in Addition and then use the settings to switch to Java 8, then you do not even need to downgrade anything. Apart from that, I guess your solution might not be connected with the thread here, since removing the code folder will most likely simply do what cleaning the whole workspace folder is doing, and this has not helped me to answer the question here. – questionto42standswithUkraine Sep 16 '20 at 19:18
  • Yeah, I knew these solutions, but updating is not good all the time :) No, not the same I tried deleting workspace too. The matter is that if u install vscode in another laptop and u use it as you want it will work, so the problem is not in your code it is in your vscode so removing vscode completly will solve it. – hamza Sep 17 '20 at 19:43
  • Yes, it is not a problem of the code but a vscode problem. Still, solving the errors of project A solved the errors in project B and let me get back to opening just project B. I remember that I could reproduce this. That is why you only add here what is already known: that it is a problem in vscode which wants to open two merged projects A and B, and it does not go back to opening just one project at a time, even if you clean the workspace. Re-installing vscode will then get rid of this merged A+B project that is expected by vscode. Yet: you can probably solve it faster with the other answers. – questionto42standswithUkraine Sep 18 '20 at 09:25
0

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.

Uwe Allner
  • 3,265
  • 9
  • 37
  • 48
  • Did you copy files to your new folder, or did you just copy code into new files in that new folder? – questionto42standswithUkraine May 03 '22 at 19:51
  • created a new file in new folder and copied the code from old file to new file. I kept no traces from old folder/file into new folder – Shreyas Joshi May 08 '22 at 18:04
  • This is true, yet, it is already said in the question that this is a workaround: `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.` And it is not a nice workaround since you have to do a lot of copy and paste if you have a big project. – questionto42standswithUkraine May 08 '22 at 21:44