Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse: The declared package does not match the expected package

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?

like image 748
HenryNguyen Avatar asked Oct 02 '11 19:10

HenryNguyen


People also ask

How do you resolve the declared package does not match the expected package?

First, let's make sure that the package declaration and relative source file path match. If that's already the case, we can try to close and reopen the project again. Sometimes the IDE may be out of sync with the project on disk and needs to reimport the files, resolve dependencies and successfully recompile.

How to resolve package error in eclipse?

Go to src folder of the project and copy all the code from it to some temporary location and build the project. And now copy the actual code from temporary location to project src. And run the build again. Problem will be resolved.

What is package declaration in Java?

The package statement should be the first line in the source file. There can be only one package statement in each source file, and it applies to all types in the file. If a package statement is not used then the class, interfaces, enumerations, and annotation types will be placed in the current default package.

How do I create a Java package?

To create a package, you choose a name for the package (naming conventions are discussed in the next section) and put a package statement with that name at the top of every source file that contains the types (classes, interfaces, enumerations, and annotation types) that you want to include in the package.


1 Answers

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".

like image 101
Jon Skeet Avatar answered Sep 22 '22 06:09

Jon Skeet