【问题标题】:java.lang.NoClassDefFoundError Erro with a maven dependency on EJB project in Jboss 7java.lang.NoClassDefFoundError Erro 依赖于 Jboss 7 中的 EJB 项目
【发布时间】:2014-12-15 22:27:46
【问题描述】:

我有一个链接到 War 和 Ejb 项目的 EAR 项目。

我正在尝试通过 pom 添加 Xstream 依赖做 EJB 项目:

    <dependency>
        <groupId>com.thoughtworks.xstream</groupId>
        <artifactId>xstream</artifactId>
        <version>1.4.7</version>
    </dependency>

项目编译正常,并将 Ejbs jar 部署到 EAR 部署目录:

             Pasta de C:\dev\jboss-as-web-7.0.1.Final\standalone\deployments\ProjectXYZWebEAR.ear

            20/10/2014  18:03    <DIR>          .
            20/10/2014  18:03    <DIR>          ..
            09/04/2012  19:40           443.432 antlr-2.7.6.jar
            09/04/2012  19:40            43.033 asm-3.1.jar
            09/04/2012  19:40           608.376 c3p0-0.9.1.jar
            09/04/2012  19:40           278.682 cglib-2.2.jar
            09/04/2012  19:40           559.366 commons-collections-3.1.jar
            09/04/2012  19:40            94.636 commons-dbcp-20030825.184428.jar
            09/04/2012  19:40           159.509 commons-io-2.0.1.jar
            09/04/2012  19:40           243.016 commons-lang-2.2.jar
            09/04/2012  19:40            38.815 commons-pool-20030825.183949.jar
            09/04/2012  19:40           313.898 dom4j-1.6.1.jar
            28/04/2014  14:13            45.024 hamcrest-core-1.3.jar
            23/08/2012  10:32           365.546 hibernate-annotations-3.5.6-Final.jar
            23/08/2012  10:31             6.413 hibernate-c3p0-3.6.8.Final.jar
            09/04/2012  19:40            71.283 hibernate-commons-annotations-3.2.0.Final.jar
            23/08/2012  10:32         3.119.425 hibernate-core-3.6.8.Final.jar
            23/08/2012  10:32           426.283 hibernate-entitymanager-3.6.8.Final.jar
            09/04/2012  19:40           100.884 hibernate-jpa-2.0-api-1.0.0.Final.jar
            23/08/2012  10:31           366.592 hibernate-validator-4.2.0.Final.jar
            09/04/2012  19:40           446.045 javassist-3.1.jar
            09/04/2012  19:40            15.071 jta-1.1.jar
            20/10/2014  18:03    <DIR>          META-INF
            20/10/2014  18:03    <DIR>          ProjectXYZWebEJB-2.27.46.7.jar
            20/10/2014  18:03    <DIR>          ProjectXYZWebWAR-2.27.46.7.war
            09/04/2012  19:40            25.689 slf4j-api-1.6.2.jar
            28/04/2014  14:13             7.668 slf4j-simple-1.6.2.jar
            09/04/2012  19:40            47.433 validation-api-1.0.0.GA.jar
            20/10/2014  10:05             7.188 xmlpull-1.1.3.1.jar
            09/04/2012  19:45            24.956 xpp3_min-1.1.4c.jar
            20/10/2014  10:05           531.571 xstream-1.4.7.jar

但在运行时调用无状态 EJB 方法时出现此错误: 18:14:49,977 错误 [stderr] (http--0.0.0.0-8084-2) 原因:java.lang.ClassNotFoundException: com.thoughtworks.xstream.XStream 来自 [模块“deployment.PortalScopeWebEAR.ear.PortalScopeWebEJB-2.27 .46.7.jar:main" 来自服务模块加载器]

如果我将 xstream 依赖添加到 WAR 项目中,我可以毫无错误地调用 Xstream 类。

Ty 提前。

【问题讨论】:

    标签: maven deployment ejb noclassdeffounderror


    【解决方案1】:

    我认为问题在于您没有将 ejb jar 依赖项放在耳朵内的 lib 文件夹中。如果您使用的是 maven ear 插件,请尝试以下操作

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-ear-plugin</artifactId>
        <version>2.6</version>
        <configuration>
            <!-- Tell Maven we are using Java EE 6 -->
            <version>6</version>
            <!-- Use Java EE ear libraries as needed. Java EE ear libraries are 
                in easy way to package any libraries needed in the ear, and automatically 
                have any modules (EJB-JARs and WARs) use them -->
            <defaultLibBundleDir>lib</defaultLibBundleDir>
            <modules>
                <ejbModule>
                    <groupId>xx</groupId>
                    <artifactId>xx</artifactId>
                    <bundleFileName>xx</bundleFileName>
                </ejbModule>
                <webModule>
                    <groupId>xx</groupId>
                    <artifactId>xx</artifactId>
                    <contextRoot>xx</contextRoot>
                </webModule>
            </modules>
        </configuration>
    </plugin>
    

    另外,检查你的 pom,你正在复制 jboss 提供的 jars.. 例如 hibernate jars.. 确保你使用 &lt;scope&gt;provided&lt;/scope&gt; 来处理这些依赖项

    【讨论】:

    • 非常感谢! ear pom中的lib解决了问题!