【问题标题】:Hibernate : Class not found error (java.lang.NoClassDefFoundError), although dependency added in MavenHibernate:找不到类错误(java.lang.NoClassDefFoundError),尽管在 Maven 中添加了依赖项
【发布时间】:2017-05-24 07:45:03
【问题描述】:

我正在尝试使用 Maven 制作一个简单的 Web 应用程序。运行时,servlet 抛出异常 java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration

根据堆栈跟踪,在创建会话工厂的第 (18) 行抛出错误 -

错误说找不到Hibernate配置文件,虽然依赖是在Maven下添加的。 pom.xml文件如下——(注意依赖hibernate-core v5.2.10.Final)

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.webapp</groupId>
  <artifactId>userinfo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>userinfo</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>5.2.10.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.1-api</artifactId>
        <version>1.0.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>5.4.1.Final</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-annotations -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-annotations</artifactId>
        <version>3.5.6-Final</version>
    </dependency>
        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-ehcache -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-ehcache</artifactId>
        <version>4.3.11.Final</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-c3p0 
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-c3p0</artifactId>
        <version>4.3.11.Final</version>
    </dependency> -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-tools 
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-tools</artifactId>
        <version>5.2.0.Final</version>
    </dependency> -->

  </dependencies>
</project>

我交叉检查,jar 也被添加到“Maven Dependency”选项卡中,并且包含 /cfg/configuration.class。

我不明白可能是什么问题。请建议我怎样才能摆脱错误。

有些东西我已经尝试过了,但都没有奏效:

  • 更改了要编译的依赖项。
  • 更新(并清理)了 Maven 项目
  • 已清理并重新启动 Tomcat 服务器
  • 论坛上的一个回答表明使用的 Hibernate 版本之间可能存在冲突。这就是注释“hibernate-tools”依赖项的原因。

任何帮助将不胜感激。谢谢

【问题讨论】:

  • 能否手动将hibernate-core-xxx.Final.jar直接复制到tomcat的lib目录下,以便tomcat在运行时加载。这不是永久修复,只是为了调试。
  • 另外,我想知道您是否在Configuration 类周围看到编译错误?

标签: java spring eclipse hibernate maven


【解决方案1】:

您的休眠工具已被注释

尝试添加这个

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-tools</artifactId>
    <version>3.2.0.ga</version>
</dependency>

【讨论】:

  • 请参考编辑。另外,我尝试将版本更改为 3.2.0.ga。没有帮助:(
  • org.hibernate.cfg.Configuration 类位于 hibernate-core-xxx.Final.jar 中,并且存在于 pom.xml 中。不要以为在项目中添加hibernate-tools 依赖就可以解决问题。
【解决方案2】:

尝试以下过程:

  1. 清理项目。
  2. 更新项目。
  3. 从 .m2 存储库中删除 /org/hibernate/ 目录。
  4. 构建项目。
  5. 部署构建。

【讨论】:

  • 谢谢。我尝试了所有这些但仍然没有帮助问题显然是因为版本冲突。我在同一个版本中添加了所有依赖项,这似乎有所帮助。
  • 这就是删除冲突版本解决问题的方法。
猜你喜欢
  • 1970-01-01
  • 2017-01-05
  • 2021-12-18
  • 2019-08-18
  • 2018-11-03
  • 1970-01-01
  • 1970-01-01
  • 2018-07-13
  • 2021-11-23
相关资源
最近更新 更多