Eclipse: "declared package does not match the expected package", but buildpath does define the declared package

rapt picture rapt · Feb 20, 2013 · Viewed 10.1k times · Source

In a maven project called my-project, eclipse keeps telling me

The declared package "com.myself" does not match the expected package "main.java.com.myself"

although I verified that in the project's build path, I have an entry:

my-project/src/main/java
    Included: **/*.java
    Excluded: (None)
    Native library location: (None)
    Ignore optional compile problems: No

What could be the problem then?

What is the easiest way to fix the problem?

I suspect that something is wrong with the eclipse settings files, because after importing the project to the workspace and converting it to a maven project, I had to change the folder structure manually in to get the conventional folder structure of a maven web project. But it could be that during those changes, something was not understood properly by eclipse.

Also, maven is able to build my project (from within eclipse, as well as from the command line).

I'm just getting those compilation error from eclipse.

P.S. I've already tried project > clean, but it did not help.

Answer

Vladimir Shtokman picture Vladimir Shtokman · Jan 15, 2014

Set my-project/src/main/java as the source folder (Project -> Properties -> Java Build Path -> Project -> Add Folder). Remove the old source folder.

Restart eclipse if necessary.

Simply put, Eclipse thinks that your project begins at my-project/src. Therefore, it expects all packages to descend from there, starting with main.java.