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

时间:2011-12-15 12:25:00

标签: java eclipse

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

23 个答案:

答案 0 :(得分:150)

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

尝试刷新Eclipse。

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

答案 1 :(得分:13)

有时会在您添加需要的jar时发生,但不包含IT所需的jar。在我的例子中添加tomcat / lib中的所有jar帮助我解决了这个问题。我正在开发一个网络应用程序。

答案 2 :(得分:11)

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

  

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

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

实际上我已经将jar添加到构建路径中,但由于某种原因,它们无法被错误地读取

  

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

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

答案 3 :(得分:4)

我将JDK升级到新版本后出现此问题。我必须在Project Properties / Java Build Path中更新对库的引用。

答案 4 :(得分:4)

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

答案 5 :(得分:3)

我还有一个案例。提供正确的项目路径,并将其导入eclipse。

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

答案 6 :(得分:2)

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

答案 7 :(得分:2)

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

答案 8 :(得分:2)

我在Eclipse Juno上遇到过这个问题,根本原因是虽然一些弹簧罐被瞬态maven依赖项包含,但它们被包含在不正确的版本中。

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

为了解决这个问题,我使用了maven依赖性排除项来避免错误版本的瞬态依赖项。

答案 9 :(得分:1)

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

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

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

复制"测试依赖项"来自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>

答案 10 :(得分:1)

错误:类型&#34;类名&#34;的层次结构是不一致的错误。

解决方案: class OtherDepJar {} - &gt;在&#34; other.dep.jar&#34;

DepJar 扩展 OtherDepJar {} - &gt;在&#34; dep.jar&#34;

class ProblematicClass扩展DepJar {} - &gt;在当前项目中。

如果dep.jar在项目的类路径中,但是other.dep.jar不在项目的类路径中,那么Eclipse将显示&#34;类型的层次结构......是不一致的错误&#34;

答案 11 :(得分:1)

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

答案 12 :(得分:1)

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

示例

class Example extends Example1 {

}

修复Example1

中的问题

答案 13 :(得分:1)

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

可以通过以下方式修复,为项目的每个类

  1. 删除每个班级中import android.[*]的所有行
  2. 重新组织您的导入:从上下文菜单中选择来源/整理进口或(CTRL + SHIFT + O)
  3. 出现提示时,选择库android.support.[*](而不是android.[*])。

答案 14 :(得分:0)

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

答案 15 :(得分:0)

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

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

在项目A中,我有界面&#34; interfaceA&#34; ... 在项目B中,我有界面&#34; interfaceB&#34;扩展&#34; interfaceA&#34; 在项目C中,我有具体的课程&#34; classC&#34;实现&#34; interfaceB&#34;

&#34;项目C&#34;包括&#34;项目B&#34;在其构建路径中,但不是&#34;项目A&#34; (这就是导致错误的原因)....包括&#34;项目A&#34;在&#34; C&#34;的构建路径中,一切都恢复正常......

答案 16 :(得分:0)

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

答案 17 :(得分:0)

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

答案 18 :(得分:0)

对我而言,它正在将Android API级别更改为使用Google API的级别

答案 19 :(得分:0)

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

答案 20 :(得分:0)

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

答案 21 :(得分:0)

在我的班级扩展了新接口的分支中进行了 git merge 后,出现了此错误。 在Eclipse的 Package Explorer 框架中刷新(F5)文件树就足够了。

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

答案 22 :(得分:0)

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

相关问题