1

我正在一个 Web 项目中研究 MAVEN 和 JPA。不知何故,我在 NoClassDefFoundError 上遇到了一个奇怪的错误,如下所示:

Caused by: java.lang.NoClassDefFoundError: WEB-INF/classes/org/huahsin/WebEngineering/Role (wrong name: org/huahsin/WebEngineering/Role)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
    at com.ibm.ws.classloading.internal.AppClassLoader.internalFindClass(AppClassLoader.java:214)
    at [internal classes]
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:171)
    at org.hibernate.internal.util.ReflectHelper.classForName(ReflectHelper.java:192)
    at org.hibernate.ejb.packaging.NativeScanner.getClassesInJar(NativeScanner.java:136)
    at org.hibernate.ejb.Ejb3Configuration.addScannedEntries(Ejb3Configuration.java:487)
    at org.hibernate.ejb.Ejb3Configuration.scanForClasses(Ejb3Configuration.java:861)
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:606)
    at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:75)
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:287)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:310)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1479)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1419)
    ... 27 more

我把我的persistence.xml放在下面src/main/resources/META-INF,内容如下:

    <persistence-unit name="WebEngineering" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>org.huahsin.WebEngineering.Role</class>
        <class>org.huahsin.WebEngineering.User</class>
        ...
        ...
    </persistence-unit>

在我的 POM.xml 中,我正在使用这些库:

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>1.3.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.2.3.Final</version>
    </dependency>

当我检查 WAR 文件时,我Role.class看到了在WEB-INF/classes/org/huahsin/WebEngineering文件夹中。我不确定我是否将持久性文件放置在正确的文件夹中,或者实际上我在 MAVEN 中使用了错误的 JPA 库?

问题已经解决了

这个问题是由于persistence.xml没有放在正确的地方造成的。它假设放在src/目录中,而不是我错误地放在resources/目录下。我最初的想法是,因为我使用的是 MAVEN,所以persistence.xml假设在resources/目录内,但实际上我错了。

4

1 回答 1

1

我认为问题可能出在父子类加载器上。检查org/huahsin/WebEngineering/Role引用的代码。您可以查看以下内容以获取更多可能性:

http://javareferencegv.blogspot.com/2013/10/debugging-javalangnoclassdeffounderror.html

于 2013-10-17T21:15:00.903 回答