Lukasz
Lukasz

Reputation: 7662

Wrong package name when using automatically added imports in Eclipse

I have several Maven projects converted into one Eclipse workspace. When I edit the code under Eclipse, I sometimes use CTRL+SHIFT+M combination of keys to automatically add imports to the classes from a project in my workspace. But somehow they are added like this:

import src.main.java.com.mycompany;

while to real package I wanted to import is com.mycompany. This has to be some configuration in Eclipse to fix this but I'm confused. However, this issue is very annoying.

EDIT:

I've forgotten to mention that Eclipse files were generated using mvn eclipse:eclipse command.

Under Eclipse project seems to be configured properly. Its source folders set like this:

And everything under Eclipse works properly except the situation when I press CTRL+SHIFT+M keys

Upvotes: 26

Views: 33443

Answers (6)

Jefe infiltrado
Jefe infiltrado

Reputation: 382

Non of the above worked for me. Finally I just changed the name of the scr folder to scr-java and this removed the package structure with scr as root package.

Upvotes: 0

Chris
Chris

Reputation: 31

First remove it. Then add it back using right click on package->build path->configure build path-> Source->Add Folder and add the entire /src/main/java tree

Upvotes: 3

fmucar
fmucar

Reputation: 14548

In eclipse;

Remove the existing source folders first. -right click -> menu -> build path -> remove from build path

then

Right click on the source folder. build path -> use as source folder.

Seems like your settings are pointing to the parent of the source folder so src is recognized as package by eclipse.

Upvotes: 19

dimitrisli
dimitrisli

Reputation: 21381

It's because eclipse is not aware of the convention over configuration filestructure Maven is following. Install the M2Eclipse plugin and File > New > Other > Maven Project for new projects or for existing ones right click on your imported project on Package Explorer > Maven > Enable Dependency Management. Once successfully done, on the Package Explorer you would see your project nicely gathered following the Maven conventonal filestructure like src/main/java, src/main/resources, src/test/java and from then on you'll start seeing your package structure hierarchy like com.mysite.myproject..

Upvotes: 4

Andreas Dolk
Andreas Dolk

Reputation: 114757

The standard source folder for Java projects is

./src

For imported maven projects, simply remove this folder from the list of source folders at the build path settigs. The correct source folder is

./src/main/java

Upvotes: 47

bmargulies
bmargulies

Reputation: 100013

You've got the wrong source folders in your build path, and it's a wonder that anything works at all.

You can use either the maven-eclipse-plugin or M2Eclipse to automate getting this right, or you can manually fix the build path to call our your actual source folders, not their great-grandparents.

Upvotes: 0

Related Questions