vscode-java-dependency icon indicating copy to clipboard operation
vscode-java-dependency copied to clipboard

Incorrect Package Warning even with correct folder structure

Open mithun2003 opened this issue 4 months ago • 2 comments

Incorrect Package error Problem: Even when my Java package names match the folder structure, VS Code shows an “Incorrect Package” error.

For example, my structure is:

src/ └── Model/ ├── Doctor.java ├── Hello1.java └── Tester.java

Inside Tester.java:

package Model; public class Tester { }

But VS Code highlights the package Model; line with: Incorrect Package (hints: 1:9-1:14)

This error goes away only after restarting VS Code, but it keeps coming back when I open or create new files.

Steps to Reproduce:

Create a folder src/Model/

Add Tester.java with package Model;

VS Code shows "Incorrect Package" error even though structure is valid.

Expected Behavior: The Java package should be recognized correctly without requiring a VS Code restart.

Actual Behavior: Incorrect package warning is shown until I restart VS Code.

mithun2003 avatar Sep 17 '25 15:09 mithun2003

Hi @mithun2003, I'm an AI Support assistant here to help with your issue. While the team reviews your request, I wanted to provide some possible tips and documentation that might help you in the meantime.

  • VS Code’s Java extension (JDT LS) must know which folders are source roots. If you just open a folder containing src/Model, it treats the workspace root as the source folder and reports any package statement as mismatched. To fix:
    • Add a build descriptor (e.g. pom.xml or build.gradle) or a Eclipse .classpath file in your project root so JDT LS auto-detects src.
    • Or explicitly mark src as a source path in your settings.json:
      "java.project.sourcePaths": [
        "src"
      ]
      
  • After changing your project layout or settings, reload the Java language server instead of fully restarting VS Code:
    • Run Java: Clean the Java language server workspace from the Command Palette, then reopen the project.
  • Ensure you open the folder that contains src, not src itself, as your workspace root.

Duplicate of:

  • eclipse-jdt.ls issue 1764: Package name not recognized when opening standalone java files
    https://github.com/eclipse-jdtls/eclipse.jdt.ls/issues/1764
Other references with low confidence
  • Issue 1406: Package containing dot character not being parsed correctly (similar “expected package ""” error)
    https://github.com/redhat-developer/vscode-java/issues/1406
  • Comment on issue 274: standalone‐file package mismatch and auto-disappear on save
    https://github.com/redhat-developer/vscode-java/issues/274#issuecomment-323831038

The team will respond to your issue shortly. I hope these suggestions are helpful in the meantime. If this comment helped you, please give it a 👍. If the suggestion was not helpful or incorrect, please give it a 👎. Your feedback helps us improve!

github-actions[bot] avatar Sep 17 '25 15:09 github-actions[bot]

Thanks for the quick reply. I’ve already tried the suggested steps:

My settings.json already contains:

"java.project.sourcePaths": ["src"],
"java.project.outputPath": "bin",

I am opening the project root (the folder containing src/, not src directly).

I also tried Java: Clean the Java language server workspace from the command palette, but the problem still persists.

The issue remains:

When I create or open a file inside src/Model/, VS Code shows Incorrect Package under package Model;.

The error disappears only after restarting VS Code, which suggests it’s not picking up source paths properly until restart.

So this doesn’t seem to be just a configuration issue, but possibly a bug in the Java Language Server’s handling of new files/folders without restart.

mithun2003 avatar Sep 17 '25 15:09 mithun2003