【问题标题】:Java: Enum : NoClassDefFoundErrorJava:枚举:NoClassDefFoundError
【发布时间】:2014-08-19 19:52:48
【问题描述】:

在我的 J2EE 应用程序中使用枚举时遇到问题。我在无状态服务 bean 中的 switch case 中使用枚举。

在运行时,我在 switch 语句中看到以下异常:

Caused by: java.lang.NoClassDefFoundError: com/comp/service/TestServiceImpl$1

这个问题已在SO 上的一个线程中进行了广泛讨论。但我没有看到任何解决此问题的解决方案。

就我而言,我使用 JBOSS EAP6.1 服务器。 JDK版本是1.7。代码是在 Eclipse IDE 中使用 Maven 构建的。并且应用程序被部署为 EAR 存档。如何在我的 EAR 存档中的类路径中添加这个额外生成的类文件?有没有其他方法可以解决这个问题?

2014 年 6 月 29 日更新: 我试图从命令行构建应用程序。然后生成这个额外的类文件。而且我能够成功部署和执行应用程序。那似乎是eclipse的错误。知道如何解决吗?

来自 EAR 项目的 pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <artifactId>demo-maven</artifactId>
        <groupId>com.comp.demo</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <artifactId>demo-ear</artifactId>
    <packaging>ear</packaging>

    <name>demo - ear</name>

    <url>www.comp.com</url>
    <licenses>
        <license>
            <name>Apache License, Version 2.0</name>
            <distribution>repo</distribution>
            <url>http://www.apache.org/licenses/LICENSE-2.0.html</url>
        </license>
    </licenses>

    <dependencies>

        <!-- Depend on the ejb module and war so that we can package them -->
        <dependency>
            <groupId>com.comp.demo</groupId>
            <artifactId>demo-web</artifactId>
            <type>war</type>
        </dependency>
        <dependency>
            <groupId>com.comp.demo</groupId>
            <artifactId>demo-service</artifactId>
            <type>ejb</type>
        </dependency>
    </dependencies>

    <build>
        <finalName>${project.parent.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ear-plugin</artifactId>
                <version>${version.ear.plugin}</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></modules>
                    <fileNameMapping>no-version</fileNameMapping>
                </configuration>
            </plugin>
            <!-- The JBoss AS plugin deploys your ear to a local JBoss EAP container -->
            <!-- Due to Maven's lack of intelligence with EARs we need to configure 
                the jboss-as maven plugin to skip deployment for all modules. We then enable 
                it specifically in the ear module. -->
            <plugin>
                <groupId>org.jboss.as.plugins</groupId>
                <artifactId>jboss-as-maven-plugin</artifactId>
                <configuration>
                    <skip>false</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <!-- When built in OpenShift the 'openshift' profile will be used when invoking mvn. -->
            <!-- Use this profile for any OpenShift specific customization your app will need. -->
            <!-- By default that is to put the resulting archive into the 'deployments' folder. -->
            <!-- http://maven.apache.org/guides/mini/guide-building-for-different-environments.html -->
            <id>openshift</id>
            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-ear-plugin</artifactId>
                        <version>${version.ear.plugin}</version>
                        <configuration>
                            <outputDirectory>deployments</outputDirectory>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

</project>

来自 ejb 项目的 pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <artifactId>demo-maven</artifactId>
        <groupId>com.comp.demo</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <artifactId>demo-service</artifactId>
    <packaging>ejb</packaging>

    <name>demo - service</name>

    <url>www.comp.com</url>
    <licenses>
        <license>
            <name>Apache License, Version 2.0</name>
            <distribution>repo</distribution>
            <url>http://www.apache.org/licenses/LICENSE-2.0.html</url>
        </license>
    </licenses>

    <dependencies>

        <!-- Declare the APIs we depend on and need for compilation. All of them 
            are provided by JBoss EAP 6 -->

        <!-- Import the EJB API, we use provided scope as the API is included in 
            JBoss EAP 6 -->
        <dependency>
            <groupId>org.jboss.spec.javax.ejb</groupId>
            <artifactId>jboss-ejb-api_3.1_spec</artifactId>
            <scope>provided</scope>
        </dependency>

        <!-- Import the CDI API, we use provided scope as the API is included in 
            JBoss EAP 6 -->
        <dependency>
            <groupId>javax.enterprise</groupId>
            <artifactId>cdi-api</artifactId>
            <scope>provided</scope>
        </dependency>

        <!-- Import the JPA API, we use provided scope as the API is included in 
            JBoss EAP 6 -->
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
            <scope>provided</scope>
        </dependency>

        <!-- JSR-303 (Bean Validation) Implementation -->
        <!-- Provides portable constraints such as @Email -->
        <!-- Hibernate Validator is shipped in JBoss EAP 6 -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <scope>provided</scope>
        </dependency>


        <dependency>
            <groupId>org.jboss.ejb3</groupId>
            <artifactId>jboss-ejb3-ext-api</artifactId>
            <version>2.0.0-redhat-2</version>
            <type>jar</type>
            <scope>provided</scope>
        </dependency>

        <!-- Test scope dependencies -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.9.5</version>
            <scope>test</scope>
        </dependency>
        <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>4.3.5.Final</version>
    <scope>test</scope>
</dependency>


        <!-- Optional, but highly recommended -->
        <!-- Arquillian allows you to test enterprise code such as EJBs and Transactional(JTA) 
            JPA from JUnit/TestNG -->
        <dependency>
            <groupId>org.jboss.arquillian.junit</groupId>
            <artifactId>arquillian-junit-container</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.jboss.arquillian.protocol</groupId>
            <artifactId>arquillian-protocol-servlet</artifactId>
            <scope>test</scope>
        </dependency>

<dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>6.0</version>
            <type>jar</type>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-ejb-plugin</artifactId>
                <version>${version.ejb.plugin}</version>
                <configuration>
                    <!-- Tell Maven we are using EJB 3.1 -->
                    <ejbVersion>3.1</ejbVersion>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <!-- The default profile skips all tests, though you can tune it to run 
                just unit tests based on a custom pattern -->
            <!-- Seperate profiles are provided for running all tests, including Arquillian 
                tests that execute in the specified container -->
            <id>default</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <version>${version.surefire.plugin}</version>
                        <configuration>
                            <skip>true</skip>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>

        <profile>
            <!-- An optional Arquillian testing profile that executes tests in your 
                JBoss EAP instance -->
            <!-- This profile will start a new JBoss EAP instance, and execute the 
                test, shutting it down when done -->
            <!-- Run with: mvn clean test -Parq-jbossas-managed -->
            <id>arq-jbossas-managed</id>
            <dependencies>
                <dependency>
                    <groupId>org.jboss.as</groupId>
                    <artifactId>jboss-as-arquillian-container-managed</artifactId>
                    <scope>test</scope>
                </dependency>
            </dependencies>
        </profile>

        <profile>
            <!-- An optional Arquillian testing profile that executes tests in a remote 
                JBoss EAP instance -->
            <!-- Run with: mvn clean test -Parq-jbossas-remote -->
            <id>arq-jbossas-remote</id>
            <dependencies>
                <dependency>
                    <groupId>org.jboss.as</groupId>
                    <artifactId>jboss-as-arquillian-container-remote</artifactId>
                    <scope>test</scope>
                </dependency>
            </dependencies>
        </profile>

    </profiles>

</project>

【问题讨论】:

  • 将 javac 创建的 all 类文件包含到存档中。 - 这似乎是在另一个类(TestServiceImpl)中声明的类(枚举),因此编译器创建(附加)类文件。
  • 我正在 Eclipse 中构建我的应用程序。而且我没有看到 maven build 生成这个类:(你有什么建议?
  • 我更新了问题,进一步分析。
  • 这里唯一的问题是 .class 文件是否在您的 .JAR、.WAR 或 .EAR 文件中以其正确的名称和包层次结构出现。
  • 当我在 maven 中构建应用程序时,这个类文件不会出现。当我从命令行构建应用程序然后在 Eclipse 中刷新工作区时,该文件是可见的。

标签: java maven jboss enums noclassdeffounderror


【解决方案1】:

我遇到了类似的问题,简单的解决方法是将枚举定义为公共而不是私有。

(没有时间验证,但我的直觉是,这会导致类不是创建为 Name$1.class 而是类似 Name$Enumname.class,这似乎是问题)

【讨论】:

    【解决方案2】:

    我有一个与您的项目配置非常相似的项目配置:eclipse、Maven、JDK 1.6、JBoss EAP6.2,当在 switch case 中使用枚举时,我遇到了 java.lang.NoClassDefFoundError 的相同问题。

    我找到了一个解决方法:生成一个 ear 文件(在我的情况下这是一场战争)并从 JBoss 管理控制台手动安装它。我注意到战争包括 1 美元所需的课程,然后你不会得到例外。

    这只是一种解决方法,但它对我有用。此解决方法的一个优点是通过 eclipse 完成的后续部署工作!

    我会听你的问题的答案,因为我想要一个真正的解决方案。

    【讨论】:

    • 您好 Fer,感谢您的更新。即使我没有解决这个问题。目前我正在从命令行构建我的 EAR。然后在 Eclipse 中刷新工作区并在 Eclipse 中部署 EAR。它对我有用。
    • 那么后续部署呢?您是否需要始终执行此过程?就我而言,只有在添加或更改与枚举相关的内容时才需要这样做。
    • 只有在 Eclipse 中清理项目或在 Enum 中进行一些更改时,我才需要进行命令行构建。如果我要进行其他一些更改,我可以继续从 eclipse 构建。
    • 你能试试这个吗?它对我有用! * 停止 JBoss * 刷新目标目录(右键单击目标目录并单击刷新) * 重新部署并启动 JBoss
    【解决方案3】:

    就我而言,为了避免 switch 语句的代码行中出现 NoClassDefFoundError,我看到的唯一解决方案是将 switch 语句替换为“else if”语句:

            if (enumValue == EnumClass.Enum1) {
                ....
            } else if (enumValue == EnumClass.Enum2) {
                ....
            } else if (enumValue == EnumClass.Enum3) {
                ....
            } else {
                ....
            }
    

    异常原因:

    Caused by: java.lang.ClassNotFoundException: com.test.services.impl.MyServiceImpl$1
        at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1358)
        at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1180)
    

    使用 Java 8、Tomcat 8.5、Spring boot 进行测试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-10
      • 2014-02-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多