redhat-developer / vscode-java

Java Language Support for Visual Studio Code
Eclipse Public License 2.0
2.07k stars 432 forks source link

[Java] The declared package "com.github.****" does not match the expected package "" #274

Closed clankill3r closed 6 years ago

clankill3r commented 7 years ago

I try to make a library for the first time in VSCode (I have done this before in IntelliJ and Eclipse). My workspace looks like this:

screen shot 2017-08-08 at 21 40 19

In LemmingsWalker.java I have: package com.github.lemmingswalker;. Which gives me the following error:

[Java] The declared package "com.github.lemmingswalker" does not match the expected package ""

Environment
fbricon commented 7 years ago

Do you have a sample project reproducing the error?

clankill3r commented 7 years ago

I figured out what it is (still seems like a bug). I wanted to make a sample project so made a copy of the directory. While opening this in VSCode the error was gone.

If I rename the original directory the error is gone as well. If I rename it back to what it was the error comes back again. So I assume there is a bug in the cache validation?

fbricon commented 7 years ago

Yeah kinda, probably. If you could provide a sample project, that'd help us figure out exactly what the issue is.

clankill3r commented 7 years ago

Well here it is: www.doekewartena.nl/temp/Remake2.zip

But it won't help. I only have the bug when the path is /Volumes/DATA/lemmings_walker/Remake2. If I rename Remake2 or if I move the folder the problem is solved.

fbricon commented 7 years ago

Ok so it seems whenever you create a new java file, it's linked to the default package initially. Once you add the package declaration to that java file, the linked resource behind the scene should be updated to the proper package.

fbricon commented 7 years ago

@snjeza: can you see if we could link the parent folder to the default project instead of creating a linked resource to the file. That'd make all files in the folder/package automatically available to the classpath, instead of having to open each one of them manually (i.e the current fake compilation unit hack)

snjeza commented 7 years ago

@fbricon I will try

gorkem commented 7 years ago

@snjeza can we close this one?

snjeza commented 7 years ago

@gorkem I don't have a permission to do it.

gorkem commented 7 years ago

@snjeza oops. Fixed you should be able to do it now.

fbricon commented 7 years ago

When you open a standalone java file in a random directory (i.e, that doesn't belong to a project), if the class has a foo.bar package, that package name will be used by default. Now if you rename the package name and save, then the same error shows up (The declared package "foo.bar.toto" does not match the expected package "foo.bar")

snjeza commented 7 years ago

@fbricon The issue hasn't been caused by this PR. See https://github.com/eclipse/eclipse.jdt.ls/blob/master/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/JDTUtils.java#L195 I am not sure how to fix this issue except to recreate a link for such a file whenever resolving a compilation unit.

VanirDev commented 6 years ago

@fbricon Unfortunately it seems like this is still very much an issue, exactly like @clankill3r my package declaration is returning an error of The declared package "aprogrammerisneverlate.xtrex" does not match the expected package "" despite having a correct file tree to support it.

image

image

fbricon commented 6 years ago

@Vanir112 given you have class files mixed with your source code, that tells me you're not using Maven or Gradle to build your code. As a consequence your source folders are not recognized and vscode-java gives you incorrect diagnostics. Can you try to use either Maven or Gradle (which is a good thing to do either way)

clankill3r commented 6 years ago

I did use maven. If I remember correct I fixed the problem with reinstalling vscode.

2018-02-14 15:59 GMT+01:00 Fred Bricon notifications@github.com:

@Vanir112 https://github.com/vanir112 given you have class files mixed with your source code, that tells me you're not using Maven or Gradle to build your code. As a consequence your source folders are not recognized and vscode-java gives you incorrect diagnostics. Can you try to use either Maven or Gradle (which is a good thing to do either way)

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/redhat-developer/vscode-java/issues/274#issuecomment-365633022, or mute the thread https://github.com/notifications/unsubscribe-auth/AAtFWhh6cdfSq0bAr07_gE7hv3xsazLEks5tUvTKgaJpZM4OxNF7 .

james-sullivan commented 5 years ago
Annotation 2019-09-17 225947

I'm having a similar problem. Seemingly at random it just stops recognizing the packages. I tried reinstalling VS Code and the extensions, but it is still a problem

clankill3r commented 4 years ago

Can you close vscode, remove the workspace storage and then open vscode, tell me if that fixed it.

jackHedaya commented 3 years ago

I have been encountering this issue recently as well. My solution was to add

{
  "java.project.sourcePaths": [""]
}

to .vscode/settings.json.

charlesAOC commented 2 years ago

This was a problem for me too. I navigated to the configure class path tab through the "java projects" tab at the bottom left of the screen. From there, i removed all other paths apart from the default. this solved my problem. The "java.project.sourcePaths": [""]" solution does this.

jheinem1 commented 1 year ago

This is still a problem regardless of what I set my sourcePaths setting to in Gradle projects. It seems the setting is completely ignored in my case.

akarampetsos commented 6 months ago

Just in case anyone has made the same (stupid) mistake that I did and cost me around 1 hour fumbling around. Make sure your folder structure is src/some/random/package and not src/some.random.package which I wrote mechanically. I was convinced it was a cache issue because of the expected package "" in the error message.