简体   繁体   English

声明的包与预期的包“”不匹配

[英]The declared package does not match the expected package “”

I am using Eclipse and have not used Java for sometime.我正在使用 Eclipse,并且有一段时间没有使用 Java。 However, I can compile my code on the command-line just fine and generate the necessary .class files.但是,我可以在命令行上很好地编译我的代码并生成必要的.class文件。 In Eclipse, it complains that The declared package "Devices" does not match the expected package "" .在 Eclipse 中,它抱怨The declared package "Devices" does not match the expected package "" What does this mean and how can I fix it?这是什么意思,我该如何解决?

Sample code:示例代码:

package Devices;

public final class DevFrequency 
{
    public short messageID;
    public double frequency;
    public short converterID;
    public DevFrequency() 
    {
    }
    public DevFrequency(short _messageID,double _frequency,short _converterID)
    {
        messageID = _messageID;
        frequency = _frequency;
        converterID = _converterID;
    }
}

The name of my project is DeviceDDS .我的项目名称是DeviceDDS

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.当文件位置或包声明绝对没有问题时,可能会在 eclipse 中出现此错误。 Try that before spending a lot of time trying these other solutions.在花费大量时间尝试这些其他解决方案之前先尝试一下。 Sometimes eclipse just gets confused.有时 eclipse 会感到困惑。 It's worked for me on a number of occasions.它在很多场合都对我有用。 I credit the idea to Joshua Goldberg .把这个想法归功于Joshua Goldberg

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. Eclipse 期望声明的包与目录层次结构相匹配 - 因此它期望您的 Java 文件位于源根目录下名为“Devices”的目录中。 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, eg请注意,通常,包是小写的,并以反向 DNS 顺序包含您的组织名称,例如

com.foo.devices;

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:当我从 eclipse CVS 签出的项目发生此错误“声明的包与预期的包不匹配”时,一种对我有用的解决方案:

1.Right click the project in the navigation bar and click 'delete' 1.在导航栏中右键单击项目,然后单击“删除”
2.Make sure ' Delete project contents on disk ' option is NOT checked, and click OK. 2.确保“删除项目内容的磁盘”选项未被选中,然后单击确定。
3.Now after the project is deleted, go to File -> Import -> General -> Existing Projects into Workspace 3.现在删除项目后,转到文件 -> 导入 -> 常规 -> 现有项目到工作区
4.Select your workspace from the directory listing and check the box next to your project name. 4. 从目录列表中选择您的工作区并选中您的项目名称旁边的框。 Click ' Finish '单击“完成

Solution 2 : Once again I got this error with the following message解决方案 2:我再次收到此错误并显示以下消息

Eclipse build errors - java.lang.Object cannot be resolved I had to follow another route mention here and the error went away. Eclipse 构建错误 - java.lang.Object 无法解决我不得不遵循这里提到的另一条路线,错误消失了。

In the mean time, the work around is to remove the JRE System Library from the project and then add it back again.同时,解决方法是从项目中删除 JRE 系统库,然后重新添加它。 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)转到带有构建错误的项目属性(右键单击 > 属性)查看“构建路径”部分中的“库”选项卡在列表中找到“JRE 系统库”(如果缺少此错误消息,则此错误消息不是eclipse 错误,但一个配置错误的项目)
  2. Remove the "JRE System Library"删除“JRE 系统库”
  3. Hit "Add Library ...", Select "JRE System Library" and add the appropriate JRE for the project (eg. 'Workspace default JRE')点击“添加库...”,选择“JRE 系统库”并为项目添加适当的 JRE(例如“工作区默认 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 ...希望错误能得到解决......

I resolved the problem by following these steps:我按照以下步骤解决了这个问题:

  1. Select the project - Right click - java build path.选择项目-右键-java构建路径。

  2. In source tab - you change the src to src/main/java .在源选项卡中 - 您将src更改为src/main/java

  3. Eclipse will reorder all the project. Eclipse 将重新排序所有项目。

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.我通过执行refactor->move修复它并将类临时移动到另一个包,然后refactor->move回原始包。

我修复它只是将名称更改为小写,现在 Eclipse 可以识别该包。

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.这相当于将类放置在项目源文件夹中名为“Devices”的目录中。

您需要将类放在文件夹Devices

确保在项目属性中将Devices定义为源文件夹。

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.希望这可能会在某些情况下有所帮助。

I fixed this by removing an "excluding" attribute for that package in my .classpath file.我通过在我的.classpath文件中删除该包的“排除”属性来解决这个问题。 Remove the attribute, not the whole tag, or "src/java" will cease to be a source folder.删除属性,而不是整个标签,否则“src/java”将不再是源文件夹。

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

This problem got resolved by mentioning the package name通过提及名称解决了这个问题

I moved my file Test_Steps.java which was under package stepDefinition我移动了位于 stepDefinition包下的文件Test_Steps.java

在此处输入图片说明

by just adding the package stepDefinition the problem got resolved只需添加包 stepDefinition问题就解决了

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.添加它已解决问题,错误不再出现。

在此处输入图片说明

确保您创建了正确的包。您可能有机会创建文件夹而不是包

  1. Create directory [your.project.name] in workspace root directory of your project.在项目的工作区根目录中创建目录 [your.project.name]。

  2. Copy *.java from "src" to that directory.将 *.java 从“src”复制到该目录。

  3. Close and reopen project.关闭并重新打开项目。

This happened to me when I was checking out a project from an svn repository in eclipse.当我从 eclipse 中的 svn 存储库签出一个项目时,这发生在我身上。 There were jar files in my .m2 folder that eclipse wasn't looking at.我的 .m2 文件夹中有 Eclipse 没有查看的 jar 文件。 To fix the issue I did:为了解决这个问题,我做了:

right click project folder Configure > Convert to Maven Project右键单击项目文件夹配置>转换为Maven项目

and that solved the issue.这解决了这个问题。

有一百万个答案,但这是另一个:将文件复制到新包中,删除旧包并将新包重命名为旧包的名称。

Make sure you are not using default package.确保您没有使用默认包。 Create a new package with name 'devices' and copy this code inside it and use.创建一个名为“devices”的新包,并在其中复制此代码并使用。

I had have this sort situations when I copied classes from other packages/projects.当我从其他包/项目复制类时,我遇到过这种情况。

Menu->Project->Clean usually helps.菜单->项目->清理通常有帮助。

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.就我而言,我在问题选项卡中选择了错误标记并将其删除,因为 Java 的文件 main 方法正在正确执行。 That is some glitch in Eclipse Neon with the multitude of plugins it has installed.这是 Eclipse Neon 中安装了大量插件的一些小故障。

I faced this issue too when I had imported an existing project to eclipse.当我将现有项目导入到 Eclipse 时,我也遇到了这个问题。 It was a gradle project, but while importing I imported it as regular project by clicking General-> Existing Projects into Workspace.这是一个 gradle 项目,但在导入时,我通过单击 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为了解决这个问题,我通过 :::: 右键单击​​项目文件夹 -> 配置 -> 添加 Gradle Nature 将 Gradle 性质添加到项目中

I was using Spring Tool Suite 4. Not able to figure out the issue.我使用的是 Spring Tool Suite 4。无法找出问题所在。 The directory structure was according to the package name.目录结构根据包名。

But cleaning the project helped me.但是清理项目对我有帮助。

I had the same issue with a maven project in Eclipse IDE.我在 Eclipse IDE 中的 Maven 项目中遇到了同样的问题。 I was able to resolve it by replacing the .classpath file with the correct format.我能够通过用正确的格式替换 .classpath 文件来解决它。 After replacing close and open the project.替换后关闭并打开项目。

Sample .classpath file示例 .classpath 文件

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

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.您可能想要在项目属性中检查您的 Java '构建路径',如果在那里没有正确设置'源'目录(路径),那么这将导致其中所有类发生此错误。

So the path must specify the 'root' (containing) directory in which the sources within the package exists ...因此,路径必须指定包中的源所在的“根”(包含)目录......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM