The declared package does not match the expected package “”

前端 未结 21 2028
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-01 09:05

I am using Eclipse and have not used Java for sometime. However, I can compile my code on the command-line just fine and generate the necessary .class files. In

相关标签:
21条回答
  • 2020-12-01 09:17

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

    0 讨论(0)
  • 2020-12-01 09:17

    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.

    0 讨论(0)
  • 2020-12-01 09:19

    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.

    0 讨论(0)
  • 2020-12-01 09:19

    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"/>
    
    0 讨论(0)
  • 2020-12-01 09:21

    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>
    
    0 讨论(0)
  • 2020-12-01 09:24

    You need to have the class inside a folder Devices.

    0 讨论(0)
提交回复
热议问题