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

15

43

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.

Rosalindrosalinda answered 15/5, 2020 at 19:27 Comment(0)
L
17

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.

Lahey answered 31/7, 2020 at 8:40 Comment(3)
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.Rosalindrosalinda
Just to make sure we are talking about the same workspace. Do you mean the workspace directory like at github.com/redhat-developer/vscode-java/wiki/… --> github.com/redhat-developer/vscode-java/wiki/… or the working directory like you find it in your project folder?Rosalindrosalinda
@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).Lahey
C
58

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.

Chucklehead answered 5/11, 2020 at 0:17 Comment(5)
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.Rosalindrosalinda
Why do you say "3. restart and delete"? I would have expected it to be just "restart".Rosalindrosalinda
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?Chucklehead
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.Rosalindrosalinda
In the version 1.65 it is Java: Clean Java language server workspaceAmongst
L
17

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.

Lahey answered 31/7, 2020 at 8:40 Comment(3)
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.Rosalindrosalinda
Just to make sure we are talking about the same workspace. Do you mean the workspace directory like at github.com/redhat-developer/vscode-java/wiki/… --> github.com/redhat-developer/vscode-java/wiki/… or the working directory like you find it in your project folder?Rosalindrosalinda
@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).Lahey
T
8

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.

Trevortrevorr answered 4/7, 2021 at 22:27 Comment(0)
R
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.

Rosalindrosalinda answered 15/5, 2020 at 20:8 Comment(0)
H
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.

Hayman answered 15/7, 2022 at 1:22 Comment(0)
L
2

As of January 2024, whenever you create a .java file, such as Main.java via File -> New File ... without first creating a folder to put it in, you will see the error about the file not being part of a project and that only syntax errors will be displayed.

The simple answer is to save the file into a folder, close the file and (this is the crucial part), reopen the folder that contains the .java file, not the actual .java file.

Once you open the folder via File Open Folder ..., the issue goes away.

Verified with:

  • VSCode 1.85.2
  • Java 21.0.1 LTS
Louvenialouver answered 19/1 at 5:4 Comment(0)
J
2

2024 update

The current answers are hard to follow because they do not show their folder structure and the contents of their workspace settings.json file if any. This makes a difference and explains why some answers may not work for you.

I will explain from scratch how to setup and run multiple java projects found in the same folder. I will not be using any build tools but you can easily adapt my method if you want to use a build tool.

Prerequisites: VS Code and Extension Pack for Java.

For the sake of completeness, I am using v0.25.15 of Extension Pack for Java and VS Code v1.81.1, and Ubuntu.

Scenario

Suppose I need to store two independent java projects qu1 and qu2 inside some homework folder:

homework/
├─ qu1/
│  ├─ some java files
├─ qu2/
│  ├─ some java files

Setup

  1. Create a homework folder and open it in VS Code.
  2. Press CTRL + SHIFT + P then search and select Java: Create Java Project....
  3. When prompted for the project type, select No build tools.
  4. When prompted for a file location, simply press the ok button to create the project in the present working directory.
  5. Enter the project name qu1 then press Enter.

A new VS Code window will now open for qu1. The file structure of qu1 will be as follows:

enter image description here

Go back to the original VS Code window and repeat steps 2-5 to create project qu2.

The important part here is the .vscode/settings.json in each project which tells VS Code how to run your project:

{
    "java.project.sourcePaths": ["src"],
    "java.project.outputPath": "bin",
    "java.project.referencedLibraries": [
        "lib/**/*.jar"
    ]
}

All your Java files must be found in the src folder.

To complicate things, let's create a package hello in qu1 and use it:

qu1/src/hello/Foo.java:

package hello;

public class Foo {
    public Foo() {
        System.out.println("ok");
    }
}

qu1/src/App.java:

import hello.Foo;

public class App {
    public static void main(String[] args) throws Exception {
        Foo bar = new Foo();
        System.out.println("Hello, World from qu1!");
    }
}

The new file structure will be as follows:

enter image description here

Run a project

To run the qu1 project:

  1. Open only the homework/qu1 folder in VS Code. Your VS Code workspace should be qu1 as shown below:

enter image description here

  1. Click on any Java file inside the src folder.
  2. Press F5 to run the project (your shortcut might be different).

You will not see any warnings about non-project files.

If you want to run qu2 project, you will have to open the homework/qu2 folder in a new VS Code window.

Migrating an existing project

If you want to convert an existing Java project to this new project structure, ensure the following:

  • The .vscode/settings.json file has been correctly added to your project.
  • All your java files are found within a src folder.
  • Check if sourcePaths in the the settings.json file is set to src.
  • There are no other Java projects inside your current project.

You will have to clean the Java language server workspace before running your project.

Jacobsohn answered 21/1 at 17:18 Comment(0)
E
1

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

Evolutionist answered 10/9, 2020 at 18:34 Comment(7)
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.Rosalindrosalinda
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 worksEvolutionist
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.Rosalindrosalinda
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!Evolutionist
Your workaround can also be reached without downgrading vscode, see #63044085. 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.Rosalindrosalinda
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.Evolutionist
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.Rosalindrosalinda
R
1

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

Reiss answered 12/6, 2021 at 2:27 Comment(0)
R
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.

Ramah answered 3/5, 2022 at 19:14 Comment(3)
Did you copy files to your new folder, or did you just copy code into new files in that new folder?Rosalindrosalinda
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 folderRamah
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.Rosalindrosalinda
S
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.

Selway answered 1/7, 2022 at 11:43 Comment(0)
W
0

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

Whiskey answered 14/8, 2022 at 17:54 Comment(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.Brumal
L
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.

Lexington answered 28/11, 2022 at 21:51 Comment(1)
What is "Dev Container"? A common noun? A particular product?Cilice
L
0

In my case, I've been tweaking some of the project's configurations to get the warning and I fixed it just by reloading the IDE ( VSCode in my case )

Note: a notification pops up usually when you change the project's configurations, it will allow you to update the project automatically and save you a restart.

Project Type: Spring Boot Modification level: pom.xml ( java.version attribute )

Legislature answered 30/4 at 9:3 Comment(0)
T
0

If you're running on WSL,

make sure WSL:Ubuntu is visible in your vscode IDE enter image description here

If it doesn't, it means you're accessing the WSL project from Windows, which is why Java (and many other frameworks) couldn't detect your project.

You should open WSL project... from WSL.

If your vscode is installed in the Windows, install extension called WSL from Microsoft

Hopefully this will solve the problem encountered by some people.

Tindall answered 30/6 at 4:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.