Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong package name when using automatically added imports in Eclipse

Tags:

java

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:

  • src/test/java
  • src/test/resources
  • src/main/java
  • src/main/resources

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

like image 391
Lukasz Avatar asked Dec 22 '10 15:12

Lukasz


People also ask

How do I fix all imports in Eclipse?

1. Go to the line of unused import, press Ctrl + 1, which is an Eclipse shortcut of a quick fix. This will show a drop-down menu to fix this error and one of them will be “remove unused imports.” It will remove that import statement from the Java file.

How do I set the default package in Eclipse Java?

They appear under a node in the hierarchy called "default package". This is because if you don't specify a package for your class, it will be grouped with all other non-packaged classes in this default package. You can add a new package in Eclipse by right-clicking on your project and selecting New > Package.


2 Answers

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 
like image 185
Andreas Dolk Avatar answered Sep 29 '22 13:09

Andreas Dolk


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.

like image 42
fmucar Avatar answered Sep 29 '22 14:09

fmucar