Eclipse: The declared package does not match the e

2019-02-03 20:55发布

I have a problem importing an external project. I go File -> Import... -> Existing Projects into Workspace, choose the folder where the project is located and everything is imported - but the package names of the project don't seem to be what Eclipse expects. The package names all have a prefix:

prefix.packagename1
prefix.packagename2

etc.

But Eclipse expects

src.prefix1.prefix.packagename1
src.prefix1.prefix.packagename2

etc. because the directory is src/prefix1/prefix/package1

I don't really want to mess around with external code. How do I tell Eclipse to ignore the directory "src/prefix1"? Or what else can I do?

11条回答
ゆ 、 Hurt°
2楼-- · 2019-02-03 21:39

The only thing that worked for me is deleting the project and then importing it again. Works like a charm :)

查看更多
倾城 Initia
3楼-- · 2019-02-03 21:43

Just go into the build path and change the source path to be src/prefix1 instead of src.

It may be easiest to right-click on the src directory and select "Build Path / Remove from build path", then find the src/prefix1 directory, right-click it and select "Build Path / Use as source folder".

查看更多
趁早两清
4楼-- · 2019-02-03 21:44

I just ran into this problem, and since Mr. Skeet's solution did not work for me, I'll share how I solved this problem.

It turns out that I opened the java file under the 'src' before declaring it a source directory.

After right clicking on the 'src' directory in eclipse, selecting 'build path', and then 'Use as Source Folder'

Close and reopen the already opened java file (F5 refreshing it did not work).

Provided the path to the java file from "prefix1" onwards lines up with the package in the file (example from the requester's question prefix1.prefix.packagename2). This should work

Eclipse should no longer complain about 'src.'

查看更多
迷人小祖宗
5楼-- · 2019-02-03 21:47

I get this problem in Eclipse sometimes when importing an Android project that does not have a .classpath file. The one that Eclipse creates is not exactly the same one that Android expects. But, the Android .classpath files are usually all relative, so I just copy a correct .classpath file from another project over the incorrect .classpath. I've created a video that shows how I do this: https://www.youtube.com/watch?v=IVIhgeahS1Ynto

查看更多
Melony?
6楼-- · 2019-02-03 21:51

If you have imported an existing project, then just remove your source folders and then add them again to build path, and restart eclipse. Most of the times eclipse will keep showing the error till you restart.

查看更多
登录 后发表回答