Eclipse: The declared package does not match the expected package
Asked Answered
W

14

90

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?

Windsor answered 2/10, 2011 at 19:31 Comment(1)
I added package (name of package under which the file to be ) at the begining of fileEbro
C
108

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

Compute answered 2/10, 2011 at 19:35 Comment(7)
How do I undo whatever that just did? I clicked on "Build Path / Use as source folder" and nothing works anymore. Edit: The answer to my question is to go into "Configure Build Path" and remove the funky thing from the list that isn't src/ or gen/Pyne
Note that Jon's suggestion works but might need a restart of eclipse.Denisse
!! If you still see the error after changing the source folder, build the project. Eclipse sometimes makes me want to smash something.Tasset
Close/Open prj worked for me... Eclipse is big tool, it can forget things sometimesNegligent
And if the suggestion (by Jon Skeet) still does not work, exit and restart Eclipse after changing the build path.Pily
I would first discuss it with my team, someone could not like this after you have pushed(git) or committed(svn) to head. Unless you are 100% sure that everyone work with ecplise, or you are not going to commit those 'package' statementRicer
This Im posting in 2018 and fixed my irritating problem as well. Cheers!Morpheme
O
75

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

Outright answered 18/1, 2013 at 16:27 Comment(3)
This was it. Pretty simple but got me stuck for a while. Thanks!Poison
After setting the build path closing and reopening the file was necessary for me as well. Thanks!Delinda
Yes! Worked for me as well.Payroll
C
11

Move your problem *.java files to other folder.

Click 'src' item and press "F5".

Red crosses will dissaperar.

Return your *.java files to "package path", click 'src' item and press "F5".

All should be ok.

Compton answered 18/11, 2012 at 20:35 Comment(0)
G
2

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.

Gomez answered 2/9, 2013 at 12:33 Comment(0)
C
2

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

Cobaltite answered 25/8, 2017 at 13:43 Comment(0)
G
1

Happens for me after failed builds run outside of the IDE. If cleaning your workspace doesn't work, try: 1) Delete all projects 2) Close and restart STS/eclipse, 3) Re-import the projects

Gobble answered 29/12, 2016 at 23:35 Comment(0)
B
0

Suppose your project has a package like package name1.name2.name3.name4 (declared package)

Your package explorer shows
package top level named name1.name2
sub packages named name3.name4

You will have errors because Eclipse extracts the package name from the file directory structure on disk starting at the point you import from.

My case was a bit more involved, perhaps because I was using a symbolic link to a folder outside my workspace.

I first tried Build Path.Java Build Path.Source Tab.Link Source Button.Browse to the folder before name1 in your package.Folder-name as you like (i think). But had issues.

Then I removed the folder from the build path and tried File > Import... > General > File System > click Next > From Directory > Browse... to folder above name1 > click Advanced button > check Create links in workspace > click Finish button.

Bothersome answered 18/8, 2012 at 13:41 Comment(1)
True. In my case, the project name had a postfix added to it, -2.0.0-SNAPSHOT. Removed that prefix (rename project..) and everything worked.Applejack
T
0

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

Toilet answered 22/12, 2014 at 14:28 Comment(0)
A
0

I happened to have the same problem just now. However, the first few answers don't work for me.I propose a solution:change the .classpath file.For example,you can define the classpathentry node's path like this: path="src/prefix1/java" or path="src/prefix1/resources". Hope it can help.

Amadeus answered 20/12, 2017 at 7:39 Comment(0)
H
0

For me the issue was that I was converting an existing project to maven, created the folder structures according to the documentation and it was showing the 'main' folder as part of the package. I followed the instructions similar to Jon Skeet / JWoodchuck and went into the Java build path, removed all broken build paths, and then added my build path to be 'src/main/java' and 'src/test/java', as well as the resources folders for each, and it resolved the issue.

Hypocrisy answered 1/10, 2019 at 16:23 Comment(0)
M
0

The build path should contain the path 'till before' that of the package name.

For eg, if the folder structure is: src/main/java/com/example/dao If the class containing the import statement'package com.example.dao' complains of the incorrect package error, then, the build path should include:src/main/java This should solve the issue.

Musculature answered 4/11, 2019 at 4:57 Comment(0)
V
0

In case you are using a pom.xml file, mine had the following structure:

<build>
    ... 
    <sourceDirectory>src</sourceDirectory>
    ...

I've changed mine to src/main/java and it finally detected the source files.

Vaduz answered 17/4 at 13:52 Comment(0)
C
-1
  1. Right click on the external folder which is having package
src.prefix1.prefix.packagename1 
src.prefix1.prefix.packagename2
  1. Click Build path --> Remove from build path.

  2. Now go the folder prefix1 in the folder section of your project.

  3. Right click on it --> Build path --> Use as source folder.

  4. Done. The package folder wont show any error now. If it still shows, just restart the project.

Connection answered 30/1, 2016 at 7:44 Comment(0)
S
-1

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.

Note: This is specific to eclipse.

Sergio answered 15/6, 2016 at 12:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.