【问题标题】:Eclipse compilation error: The hierarchy of the type 'Class name' is inconsistentEclipse 编译错误:“类名”类型的层次结构不一致
【发布时间】:2012-01-21 03:34:27
【问题描述】:

我下载了一些用 Java 编写的开源软件,并尝试使用 Eclipse 编译它。 我收到错误:“'类名'类型的层次结构不一致”在某些文件中。 是什么导致了这些错误,我该如何解决?

【问题讨论】:

    标签: java eclipse


    【解决方案1】:

    这意味着您正在尝试实现一个不存在的接口,或者您正在扩展一个不存在的类。

    尝试刷新您的 Eclipse。

    如果它不起作用,则可能意味着您引用了一个不在构建路径中的 JAR。 检查项目的类路径并验证包含接口或类的 jar 是否在其中。

    【讨论】:

    • 在类似的情况下,我有一个 Maven 依赖项,导致 Spring Tool Suite 中出现此错误,解决方案是对相关依赖项执行Maven > Download Source
    • 还要检查 Parent 是否正在编译。就我而言,我知道超类存在,但实际上并没有正确编译。
    • 我有一个类扩展了一个抽象类,它正在实现一个缺失的(从 Eclipse 中重命名)接口
    • 在我的例子中,我扩展了一个在类路径中的类(一个 jar),但它扩展了第三个类,该类位于另一个 jar 中,而该 jar 不在我的类路径中。
    • 这可能是由层次结构中的任何不正确实现引起的,而不仅仅是直接实现。例如,A 扩展 B,B 实现 C...如果 B 缺少 C 导入,您将在 A 中看到错误消息。
    【解决方案2】:

    检查您的错误(标签“标记”)。我也有以下错误:

    无法读取项目中所需库的存档...

    当它被修复后,“不一致错误”消失了。

    实际上我已经在构建路径中添加了 jar,但由于某种原因,它们无法读取并出现错误

    项目中所需库的存档无法读取或不是有效的 ZIP 文件

    所以我将它们添加为“外部罐子”。这很有帮助,所有的编译问题都没有了!

    【讨论】:

      【解决方案3】:

      如果您在类路径中的库文件中的某个类引用了可能在另一个 jar 文件中的不存在的类,您将看到此错误。在这里,当我没有添加 org.springframework.beans-3.1.2.RELEASE.jar 并从 org.springframework.jdbc.core.support.JdbcDaoSupport 扩展一个类时收到此错误,该类位于我的类路径的 org.springframework.jdbc-3.1.2.RELEASE.jar 中。

      【讨论】:

        【解决方案4】:

        当您添加一个您需要的 jar,但不包括 IT 需要的 jar 时,有时会发生这种情况。就我而言,在 tomcat/lib 中添加所有 jars 帮助我解决了这个问题。我正在开发一个网络应用程序。

        【讨论】:

        • 谢谢,这是我的问题。我包含了 GWT 库,但缺少 Java servlet API jar(在这种情况下来自 Jetty 的 servlet-api-3.1.jar)。
        【解决方案5】:

        问题可能是您包含了不正确的 jar。我遇到了同样的问题,原因是我在项目的构建路径中包含了不正确的默认 JRE 库。我已经安装了另一个版本的 Java,并且包含了不同版本的 Java 的 JRE 文件。 (我已经在我的系统中安装了 JRE 1.6,并且由于以前安装了 Java,JRE 库 1.7 包含在构建路径中)您可以检查构建路径中包含的 JRE 库是否是正确的版本,即。您在系统中安装的 Java 版本。

        【讨论】:

          【解决方案6】:

          我在 Eclipse Juno 上遇到过这个问题,根本原因是虽然一些 spring jar 包含在临时 maven 依赖项中,但它们包含在不正确的版本中。

          因此,您应该检查使用模块化框架作为 spring 是否每个模块(或至少是最重要的:核心、bean、上下文、aop、tx 等)都在相同的版本中。

          为了解决这个问题,我使用了 maven 依赖排除来避免不正确的临时依赖版本。

          【讨论】:

            【解决方案7】:

            我在将 JDK 升级到新版本后遇到了这个问题。我必须更新项目属性/Java 构建路径中对库的引用。

            【讨论】:

              【解决方案8】:

              我还有一个案例。给出正确的项目路径,并导入到eclipse中。

              然后转到项目--> 清理--> 清理所有项目。

              【讨论】:

                【解决方案9】:

                对我来说,它是将 Android API 级别更改为使用 Google API 的级别

                【讨论】:

                  【解决方案10】:

                  对我来说,问题是由于错误的进口。实际上,添加 v7 支持库后需要更新导入。

                  可以通过以下方式修复它,对于您项目的每个类

                  1. 删除每个类中所有带有import android.[*]的行
                  2. 重新组织导入:从上下文菜单中选择 Source/Organize Imports 或 (CTRL+SHIFT+O)
                  3. 出现提示时,选择库 android.support.[*](而不是 android.[*])。

                  【讨论】:

                    【解决方案11】:

                    我也遇到了这个问题...我发现抛出此异常的类的层次结构无法通过eclipse一直追溯到其根类...我解释一下:

                    就我而言,我有 3 个 java 项目:A、B 和 C... 其中 A 和 B 是 maven 项目,C 是常规 java eclipse 项目...

                    在项目 A 中,我有接口“interfaceA”... 在项目B中,我有扩展“interfaceA”的接口“interfaceB” 在项目C中,我有实现“interfaceB”的具体类“classC”

                    “项目 C”在其构建路径中包含“项目 B”,但不包含“项目 A”(这就是错误的原因)....在构建路径中包含“项目 A”后“C”,一切恢复正常……

                    【讨论】:

                      【解决方案12】:

                      这绝对是因为缺少我的 maven pom.xml 中没有的依赖项。

                      例如,我想为我的阔叶电子商务演示网站的实现创建集成测试。

                      我已经包含了一个带有来自 broadleaf commerce 的集成测试的 broadleaf jar,以便重用它们的配置文件和基础测试类。该项目有其他我没有包含的测试依赖项,并且我收到了“不一致的层次结构”错误。

                      在从 broadleaf/pom.xml 复制“测试依赖项”以及为 Broadleaf/pom.xml 中的每个依赖项提供版本的相关属性变量后,错误消失了。

                      属性是:

                          <geb.version>0.9.3</geb.version>
                          <spock.version>0.7-groovy-2.0</spock.version>
                          <selenium.version>2.42.2</selenium.version>
                          <groovy.version>2.1.8</groovy.version>
                      

                      依赖项是:

                      <dependency>
                                  <groupId>org.broadleafcommerce</groupId>
                                  <artifactId>integration</artifactId>
                                  <type>jar</type>
                                  <classifier>tests</classifier>
                                  <scope>test</scope>
                              </dependency>
                              <dependency>
                                  <groupId>org.broadleafcommerce</groupId>
                                  <artifactId>broadleaf-framework</artifactId>
                                  <version>${blc.version}</version><!--$NO-MVN-MAN-VER$ -->
                                  <classifier>tests</classifier>
                              </dependency>
                              <dependency>
                                  <groupId>com.icegreen</groupId>
                                  <artifactId>greenmail</artifactId>
                                  <version>1.3</version>
                                  <type>jar</type>
                                  <scope>test</scope>
                              </dependency>
                              <dependency>
                                  <groupId>junit</groupId>
                                  <artifactId>junit</artifactId>
                                  <version>4.11</version>
                                  <scope>test</scope>
                              </dependency>
                              <dependency>
                                  <groupId>org.easymock</groupId>
                                  <artifactId>easymock</artifactId>
                                  <version>2.5.1</version>
                                  <type>jar</type>
                                  <scope>test</scope>
                              </dependency>
                              <dependency>
                                  <groupId>org.easymock</groupId>
                                  <artifactId>easymockclassextension</artifactId>
                                  <version>2.4</version>
                                  <type>jar</type>
                                  <scope>test</scope>
                              </dependency>
                              <dependency>
                                  <groupId>org.testng</groupId>
                                  <artifactId>testng</artifactId>
                                  <version>5.9</version>
                                  <type>jar</type>
                                  <classifier>jdk15</classifier>
                                  <scope>test</scope>
                              </dependency>
                              <dependency>
                                  <groupId>org.codehaus.groovy</groupId>
                                  <artifactId>groovy-all</artifactId>
                                  <version>${groovy.version}</version>
                                  <scope>test</scope>
                              </dependency>
                              <dependency>
                                  <groupId>org.gebish</groupId>
                                  <artifactId>geb-core</artifactId>
                                  <version>${geb.version}</version>
                                  <scope>test</scope>
                              </dependency>
                              <dependency>
                                  <groupId>org.gebish</groupId>
                                  <artifactId>geb-spock</artifactId>
                                  <version>${geb.version}</version>
                                  <scope>test</scope>
                              </dependency>
                              <dependency>
                                  <groupId>org.spockframework</groupId>
                                  <artifactId>spock-core</artifactId>
                                  <version>${spock.version}</version>
                                  <scope>test</scope>
                              </dependency>
                              <dependency>
                                  <groupId>org.seleniumhq.selenium</groupId>
                                  <artifactId>selenium-support</artifactId>
                                  <version>${selenium.version}</version>
                                  <scope>test</scope>
                              </dependency>
                              <dependency>
                                  <groupId>org.seleniumhq.selenium</groupId>
                                  <artifactId>selenium-firefox-driver</artifactId>
                                  <version>${selenium.version}</version>
                                  <scope>test</scope>
                              </dependency>
                              <dependency>
                                  <groupId>org.seleniumhq.selenium</groupId>
                                  <artifactId>selenium-chrome-driver</artifactId>
                                  <version>${selenium.version}</version>
                                  <scope>test</scope>
                              </dependency>
                        <!-- Logging -->
                                  <dependency>
                                      <groupId>log4j</groupId>
                                      <artifactId>log4j</artifactId>
                                      <version>1.2.12</version>
                                      <type>jar</type>
                                      <scope>test</scope>
                                  </dependency>
                                  <dependency>
                                      <groupId>org.slf4j</groupId>
                                      <artifactId>slf4j-log4j12</artifactId>
                                      <version>1.6.1</version>
                                      <type>jar</type>
                                      <scope>test</scope>
                                  </dependency>
                                  <dependency>
                                      <groupId>org.slf4j</groupId>
                                      <artifactId>jcl-over-slf4j</artifactId>
                                      <version>1.6.1</version>
                                      <type>jar</type>
                                      <scope>test</scope>
                                  </dependency>
                                  <dependency>
                                      <groupId>org.slf4j</groupId>
                                      <artifactId>slf4j-api</artifactId>
                                      <version>1.6.1</version>
                                      <type>jar</type>
                                      <scope>test</scope>
                                  </dependency>
                                  <dependency>
                                      <groupId>org.hsqldb</groupId>
                                      <artifactId>hsqldb</artifactId>
                                      <version>2.3.1</version>
                                      <type>jar</type>
                                      <scope>test</scope>
                                  </dependency>
                      

                      【讨论】:

                        【解决方案13】:

                        我有一个在 OSGi 项目中扩展 LabelProvider 的类,出现错误。解决方案是:将 org.eclipse.jface 添加到 manifest.mf 中所需的插件中,而不是导入像 org.eclipse.jface.viewers 这样的单个包

                        【讨论】:

                          【解决方案14】:

                          如果扩展类出现问题,则会显示上述错误消息。

                          例子

                          class Example extends Example1 {
                          
                          }
                          

                          修复Example1中的问题

                          【讨论】:

                            【解决方案15】:

                            您应该清理项目,或者重新启动 Eclipse。

                            【讨论】:

                              【解决方案16】:

                              我有完全相同的问题标记,并通过从实际上是第一个实现(“超级”是抽象方法)而不是覆盖的方法中删除 @Override 注释来解决它。

                              【讨论】:

                                【解决方案17】:

                                在我的例子中,许多类中的导入引用都包含一个额外的词。我通过编辑所有文件以获得正确的导入来解决它。我开始手动进行编辑。但是当我看到这种模式时,我在 Eclipse 中使用 find..replace 将其自动化。这解决了错误。

                                【讨论】:

                                  【解决方案18】:

                                  如果你只是导入 eclipse 项目 1.进入项目属性下的java构建路径设置。 2. 如果 JRE 系统库附有错误符号,请双击它以打开编辑库窗口 3. 将执行环境更改为系统的正确 java 版本,或通过检查分配给它们的单选按钮选择编辑其他设置。 4.点击完成

                                  【讨论】:

                                    【解决方案19】:

                                    错误:“类名”类型的层次结构不一致错误。

                                    解决方案: 类 OtherDepJar {} --> 在 "other.dep.jar" 内。

                                    class DepJar extends OtherDepJar {} --> 在 "dep.jar" 内。

                                    class ProblematicClass extends DepJar {} --> 在当前项目中。

                                    如果dep.jar在项目的classpath中,而other.dep.jar不在项目的classpath中,Eclipse会显示“The hierarchy of the type ... is compatible error”

                                    【讨论】:

                                      【解决方案20】:

                                      在 Eclipse 中导入 GWT 项目而不安装“Google Plugin for Eclipse”时,会发生这种情况。安装“Google Plugin for Eclipse”后,此错误将消失。

                                      【讨论】:

                                        【解决方案21】:

                                        右键单击项目文件夹并选择“Java Build Path”。在“Java Build Path”下,您应该能够看到库。 Eclipse 将在任何这些库中显示错误。解决这些问题将有助于解决问题。

                                        【讨论】:

                                          【解决方案22】:

                                          在我的类扩展了新接口的分支中执行了一些 git merge 后,我遇到了这个错误。 在 Eclipse 的 Package Explorer 框架中刷新 (F5) 文件树就足够了。

                                          似乎 Eclipse 没有正确更新所有内容,因此这些类正在扩展一个尚不存在的接口。刷新后,所有错误都消失了。

                                          【讨论】:

                                            【解决方案23】:

                                            我不得不从从 IBM 获得并使用 IBM JDK 8 的 Eclipse Oxygen 切换到 Eclipse Photon 和 Oracle JDK 8。我正在为 进行 Java 自定义。

                                            【讨论】:

                                              猜你喜欢
                                              • 1970-01-01
                                              • 2012-03-26
                                              • 2013-12-06
                                              • 1970-01-01
                                              • 1970-01-01
                                              • 1970-01-01
                                              • 1970-01-01
                                              • 1970-01-01
                                              • 1970-01-01
                                              相关资源
                                              最近更新 更多