37

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 Visual Studio 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 Studio Code warning?

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

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
questionto42
  • 7,175
  • 4
  • 57
  • 90

11 Answers11

46

Try cleaning the Java language server workspace:

  1. Cmd + Shift + P (Ctrl + Shift + P on Windows) to show the command palette
  2. Choose "Java: Clean the Java language server workspace"
  3. Restart and Delete

It just worked for me.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
bkir
  • 469
  • 1
  • 4
  • 2
  • 1
    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. – questionto42 Nov 05 '20 at 07:56
  • 1
    Why do you say "3. restart and delete"? I would have expected it to be just "restart". – questionto42 Nov 05 '20 at 07:58
  • 1
    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. – questionto42 Nov 06 '20 at 20:14
  • 1
    In the version 1.65 it is *Java: Clean Java language server workspace* – Ravindra Gullapalli Mar 10 '22 at 07:51
15

I got the same warning simply because I had two Java (Maven) projects in the same Visual Studio Code workspace. Once I moved projectA out of the workspace, the warning for projectB was gone.

WorkspaceRoot
│   projectA
└───projectB

My current solution is to have one Java (Maven) project for one workspace, i.e, one Maven project per Visual Studio Code workspace.

My guess is that Visual Studio Code treats all Java projects inside the same workspace as as one project and hence, the projects interfering with each other.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
kohane15
  • 809
  • 12
  • 16
  • 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. – questionto42 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? – questionto42 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
6

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 Visual Studio Code instead. That seems to have merged the two 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. It seems as if Visual Studio 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 Visual Studio Code considered both as one project.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
questionto42
  • 7,175
  • 4
  • 57
  • 90
5

I struggled with this for a long time, but I 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 Ctrl + 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mohit Arora
  • 86
  • 1
  • 7
2

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

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

How I fixed it...

I went to Java in Visual Studio Code and read sections "Working with Java source files" and "Working with Java projects". Basically, when working with "Java projects" in Visual Studio 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 Visual Studio 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 Visual Studio 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ninja_Wafa
  • 31
  • 3
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
  • 4,115
  • 6
  • 33
  • 42
Thuan Tran
  • 351
  • 2
  • 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. – questionto42 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. – questionto42 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. – questionto42 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. – questionto42 Sep 18 '20 at 09:25
0

I have used the Visual Studio Code editor first-time for Java and I faced this issue with my simple Hello, World! 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 Visual Studio Code workspace, and then created a Java file in that newly created workspace.

When I ran that file, it worked without any warnings or errors.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Did you copy files to your new folder, or did you just copy code into new files in that new folder? – questionto42 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. – questionto42 May 08 '22 at 21:44
0

Instead of directly opening the Main.java file with Visual Studio Code Don't do this.

Open the folder in Visual Studio 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 17 '22 at 11:10
0

I have been running it a Dev Container/Docker image and multiple rebuilds have not been helping with this error. It has turned out I have accidentally created the directory in the root of the tree the current user had no access to. It has been the source of the issue.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131