简体   繁体   English

使用hibernate-validator时出现NoClassDefFoundError

[英]NoClassDefFoundError while using hibernate-validator

I have a mavenized project, where I use JSR 303 (Bean validation). 我有一个简化的项目,在这里使用JSR 303(Bean验证)。 As reference implementation I use Hibernate-validator. 作为参考实现,我使用了Hibernate-validator。 In project I specified dependency: 在项目中,我指定了依赖项:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>4.1.0.Final</version>
    <scope>provided</scope>
</dependency>

With version 4.1.0 all works fine. 在4.1.0版中,一切正常。 But when I change it to 4.2.0. 但是当我将其更改为4.2.0时。 I get runtime error on this line: 我在此行上收到运行时错误:

ValidatorFactory factory = Validation.buildDefaultValidatorFactory();

java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/persistence/Persistence java.lang.ClassFormatError:类文件javax / persistence / Persistence中不是本机或抽象的方法中的缺少Code属性

......//more stack-trace ...... //更多堆栈跟踪

java.lang.NoClassDefFoundError: Could not initialize class com.andr.myproject.services.utils.ValidationUtils java.lang.NoClassDefFoundError:无法初始化com.andr.myproject.services.utils.ValidationUtils类

Where is problem? 问题在哪里?

UPD: Also I use Java-ee-5 dependency , not 6. UPD:我也使用Java-ee-5依赖性 ,而不是6。

UPD2: this error occurs in JUnit test UPD2:在JUnit测试中会发生此错误

Do you happen to have the following dependency in your project: 您是否在项目中具有以下依赖项:

<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>6.0</version>
</dependency>

If so, you should make sure to exclude it from your runtime classpath, as this JAR can only be used for compilation but not at runtime (it's classes are modified so they contain no method implementations). 如果是这样,则应确保将其从运行时类路径中排除,因为此JAR只能用于编译,而不能在运行时使用(它的类已修改,因此它们不包含任何方法实现)。 You can use the API JAR provided by JBoss instead: 您可以改为使用JBoss提供的API JAR:

<dependency>
    <groupId>org.jboss.spec</groupId>
    <artifactId>jboss-javaee-6.0</artifactId>
    <version>1.0.0.Final</version>
    <type>pom</type>
    <scope>provided</scope>
</dependency>

Likely adding Hibernate Core helped because it pulls in the JPA API in a separate JAR. 可能添加Hibernate Core有所帮助,因为它在单独的JAR中引入了JPA API。 You can find more information here . 您可以在此处找到更多信息。

确保maven实际上从回购中获取jar,并使用它,并且该jar中存在提到的类。

I found solution: 我找到了解决方案:

I need to add hibernate-core dependency. 我需要添加休眠核心依赖项。 Without it it wouldn't work. 没有它,就行不通。

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.0.1.Final</version>
    </dependency>

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

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