score:63

Accepted answer

eclipse expects the declared package to match the directory hierarchy - so it's expecting your java file to be in a directory called "devices" under your source root. at the moment it looks like the file is directly in your source root. so create the appropriate directory, and move the file in there.

note that conventionally, packages are in lower case and include your organization name in reverse dns order, e.g.

com.foo.devices;

score:0

make sure that you have created a correct package.you might get a chance to create folder instead of package

score:0

  1. create directory [your.project.name] in workspace root directory of your project.

  2. copy *.java from "src" to that directory.

  3. close and reopen project.

score:0

this happened to me when i was checking out a project from an svn repository in eclipse. there were jar files in my .m2 folder that eclipse wasn't looking at. to fix the issue i did:

right click project folder configure > convert to maven project

and that solved the issue.

score:0

there are a million answers, but here's another one: copy the files into a new package, delete the old package and rename the new package to the old package's name.

score:0

make sure you are not using default package. create a new package with name 'devices' and copy this code inside it and use.

score:0

i had have this sort situations when i copied classes from other packages/projects.

menu->project->clean usually helps.

score:0

in my case i selected the error marker in the problems tab and deleted it since the java's file main method was being executed correctly. that is some glitch in eclipse neon with the multitude of plugins it has installed.

score:0

i faced this issue too when i had imported an existing project to eclipse. it was a gradle project, but while importing i imported it as regular project by clicking general-> existing projects into workspace. to resolve the issue i've added gradle nature to the project by :::: right click on project folder -> configure-> add gradle nature

score:0

i was using spring tool suite 4. not able to figure out the issue. the directory structure was according to the package name.

but cleaning the project helped me.

score:0

i had the same issue with a maven project in eclipse ide. i was able to resolve it by replacing the .classpath file with the correct format. after replacing close and open the project.

sample .classpath file

<?xml version="1.0" encoding="utf-8"?>
<classpath>
    <classpathentry kind="src" output="target/classes" path="src/main/java">
        <attributes>
            <attribute name="optional" value="true"/>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
        <attributes>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry excluding="**" kind="src" output="target/classes" path="src/main/webapp">
        <attributes>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="src" output="target/test-classes" path="src/test/java">
        <attributes>
            <attribute name="optional" value="true"/>
            <attribute name="maven.pomderived" value="true"/>
            <attribute name="test" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.jre_container/org.eclipse.jdt.internal.debug.ui.launcher.standardvmtype/javase-1.8">
        <attributes>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="con" path="org.eclipse.m2e.maven2_classpath_container">
        <attributes>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="output" path="target/classes"/>
</classpath>

score:0

you might want to check your java 'build path' in the project properties, if the 'source' directory (path) is not properly setup in there, then this will cause this error to occur for all the classes therein.

so the path must specify the 'root' (containing) directory in which the sources within the package exists ...

score:1

make sure that devices is defined as a source folder in the project properties.

score:1

got the same kind of error but my package was absolutely correct. when i just closed and opened my editor, the error disappears. hope this might help in some scenarios.

score:1

i fixed this by removing an "excluding" attribute for that package in my .classpath file. remove the attribute, not the whole tag, or "src/java" will cease to be a source folder.

<classpathentry excluding="com/myproject/mypackage/mysubpackage/" kind="src" path="src/java"/>

score:2

create a new package under your project called "devices" and place your class in it. this is equivalent to the class being placed in a directory called "devices" in your project source folder.

score:2

you need to have the class inside a folder devices.

score:2

this problem got resolved by mentioning the package name

i moved my file test_steps.java which was under package stepdefinition

enter image description here

by just adding the package stepdefinition the problem got resolved

so this problem can occur when you have a package and you are not using in your class file.

adding it has resolved the problem and the error was no longer appearing.

enter image description here

score:3

i fix it just changing the name to lowercase and now eclipse recognizes the package.

score:11

i had this problem - the other classes within my package were fine, but one class had the error against it. there was nothing wrong with the package declaration.

i fixed it by doing refactor->move and moved the class to another package temporarily, then refactor->move back to the original package.

score:16

i resolved the problem by following these steps:

  1. select the project - right click - java build path.

  2. in source tab - you change the src to src/main/java.

  3. eclipse will reorder all the project.

score:49

solution 1 : one solution that worked for me when this error "the declared package does not match the expected package" occured for a project i checked-out from eclipse cvs :

1.right click the project in the navigation bar and click 'delete'
2.make sure 'delete project contents on disk' option is not checked, and click ok.
3.now after the project is deleted, go to file -> import -> general -> existing projects into workspace
4.select your workspace from the directory listing and check the box next to your project name. click 'finish'

solution 2 : once again i got this error with the following message

eclipse build errors - java.lang.object cannot be resolved i had to follow another route mention here and the error went away.

in the mean time, the work around is to remove the jre system library from the project and then add it back again. here are the steps:

  1. go to properties of project with the build error (right click > properties) view the "libraries" tab in the "build path" section find the "jre system library" in the list (if this is missing then this error message is not an eclipse bug but a mis-configured project)
  2. remove the "jre system library"
  3. hit "add library ...", select "jre system library" and add the appropriate jre for the project (eg. 'workspace default jre')
  4. hit "finish" in the library selection and "ok" in the project properties and then wait for the re-build of the project

hopefully the error will be resolved ...

score:150

try closing and re-opening the file.

it is possible to get this error in eclipse when there is absolutely nothing wrong with the file location or package declaration. try that before spending a lot of time trying these other solutions. sometimes eclipse just gets confused. it's worked for me on a number of occasions. i credit the idea to joshua goldberg.


Related Query

More Query from same tag