4

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.

GokcenG
  • 2,771
  • 2
  • 25
  • 40
rapt
  • 11,810
  • 35
  • 103
  • 145

3 Answers3

7

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.

  • After following the instructions by Valimir, Project -> Maven -> update project, in my case cleared the error message – Kemin Zhou Jan 10 '19 at 20:55
3

Looks like the project is still not in maven nature...

Follow the steps:

Right Click on the project folder -> Maven -> Update Project...

OR, open the command prompt, go to the root project directory, and type the following command:

mvn eclipse:clean eclipse:eclipse
Manoj Shrestha
  • 4,246
  • 5
  • 47
  • 67
0
  1. I copied the project folder to some place outside of my eclipse workspace.
  2. Then I deleted the project from the eclipse project explorer and the workspace.
  3. Now I created a new java project with the same name, and imported the original project that I saved at (1).
  4. Tah dah! no compilation errors.
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
rapt
  • 11,810
  • 35
  • 103
  • 145