Java:枚举:NoClassDefFoundError [英] Java: Enum : NoClassDefFoundError

查看:391
本文介绍了Java:枚举:NoClassDefFoundError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的J2EE应用程序中使用枚举时遇到问题。我在无状态服务bean中的一个交换机中使用枚举。



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

 导致:java.lang.NoClassDefFoundError:com / comp / service / TestServiceImpl $ 1 

这个问题已经在 SO 。但是我没有看到任何解决方案来解决这个问题。



在我的情况下,我使用JBOSS EAP6.1服务器。 JDK版本是1.7。使用Eclipse IDE中的Maven构建代码。应用程序部署为EAR存档。如何在我的EAR档案中的classpath中添加这个额外生成的类文件?有没有办法解决这个问题?



更新2014年6月29日
我试图从命令行构建应用程序。然后生成这个额外的类文件。我可以成功地执行应用程序。这似乎是日食的错误。任何想法如何解决?



来自EAR项目的pom.xml:

 <?xml version =1.0encoding =UTF-8?> 
< project xmlns =http://maven.apache.org/POM/4.0.0xmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexsi: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许可证版本2.0< / name>
< distribution> repo< / distribution>
< url> http://www.apache.org/licenses/LICENSE-2.0.html< / url>
< / license>
< / licenses>

<依赖关系>

<! - 依赖于ejb模块和战争,以便我们可以打包它们 - >
<依赖关系>
< groupId> com.comp.demo< / groupId>
< artifactId> demo-web< / artifactId>
< type> war< / type>
< / 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>
<! - 告诉Maven我们正在使用Java EE 6 - >
< version> 6< / version>
<! - 根据需要使用Java EE耳机库。 Java EE耳机库
可以方便地打包耳朵中所需的任何库,并且自动
有任何模块(EJB-JAR和WAR)使用它们 - >
< defaultLibBundleDir> lib< / defaultLibBundleDir>
< modules>< / modules>
< fileNameMapping> no-version< / fileNameMapping>
< / configuration>
< / plugin>
<! - JBoss AS插件将您的耳朵部署到本地JBoss EAP容器 - >
<! - 由于Maven缺乏EAR的智能,我们需要配置
的jboss-as maven插件来跳过所有模块的部署。然后,我们在耳机模块中启用
。 - >
< plugin>
< groupId> org.jboss.as.plugins< / groupId>
< artifactId> jboss-as-maven-plugin< / artifactId>
< configuration>
< skip> false< / skip>
< / configuration>
< / plugin>
< / plugins>
< / build>

<个人资料>
<个人资料>
<! - 当内置OpenShift时,调用mvn时将使用'openshift'配置文件。 - >
<! - 使用此配置文件用于您的应用程序需要的任何OpenShift特定定制。 - >
<! - 默认情况下,将生成的归档放入'deployments'文件夹。 - >
<! - 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>部署< / outputDirectory>
< / configuration>
< / plugin>
< / plugins>
< / build>
< / profile>
< / profiles>

< / project>

ejb项目中的pom.xml:

 <?xml version =1.0encoding =UTF-8?> 
< project xmlns =http://maven.apache.org/POM/4.0.0xmlns: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许可证版本2.0< / name>
< distribution> repo< / distribution>
< url> http://www.apache.org/licenses/LICENSE-2.0.html< / url>
< / license>
< / licenses>

<依赖关系>

<! - 声明我们依赖并需要编译的API。所有这些都由
由JBoss EAP 6提供,

<! - 导入EJB API,我们使用提供的范围,因为API包含在
中JBoss EAP 6 - >
<依赖关系>
< groupId> org.jboss.spec.javax.ejb< / groupId>
< artifactId> jboss-ejb-api_3.1_spec< / artifactId>
< scope>已提供< / scope>
< / dependency>

<! - 导入CDI API,我们使用提供的范围,因为API包含在
中JBoss EAP 6 - >
<依赖关系>
< groupId> javax.enterprise< / groupId>
< artifactId> cdi-api< / artifactId>
< scope>已提供< / scope>
< / dependency>

<! - 导入JPA API,我们使用提供的范围,因为API包含在
中JBoss EAP 6 - >
<依赖关系>
< groupId> org.hibernate.javax.persistence< / groupId>
< artifactId> hibernate-jpa-2.0-api< / artifactId>
< scope>已提供< / scope>
< / dependency>

<! - JSR-303(Bean验证)实现 - >
<! - 提供便携式约束,如@Email - >
<! - Hibernate Validator在JBoss EAP 6 - >
<依赖关系>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-validator< / artifactId>
< scope>已提供< / scope>
< / dependency>


<依赖关系>
< groupId> org.jboss.ejb3< / groupId>
< artifactId> jboss-ejb3-ext-api< / artifactId>
< version> 2.0.0-redhat-2< / version>
< type> jar< / type>
< scope>已提供< / scope>
< / dependency>

<! - 测试范围依赖关系 - >
<依赖关系>
< groupId> junit< / groupId>
< artifactId> junit< / artifactId>
< scope> test< / scope>
< / dependency>

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


<! - 可选,但强烈推荐 - >
<! - Arquillian允许您从JUnit / TestNG - >测试企业代码,如EJB和事务(JTA)
JPA。
<依赖关系>
< groupId> org.jboss.arquillian.junit< / groupId>
< artifactId> arquillian-junit-container< / artifactId>
< scope> test< / scope>
< / dependency>

<依赖关系>
< groupId> org.jboss.arquillian.protocol< / groupId>
< artifactId> arquillian-protocol-servlet< / artifactId>
< scope> test< / scope>
< / dependency>

<依赖关系>
< groupId> javax< / groupId>
< artifactId> javaee-api< / artifactId>
< version> 6.0< / version>
< type> jar< / type>
< scope>已提供< / scope>
< / dependency>
< / dependencies>

< build>
< finalName> $ {project.artifactId}< / finalName>
< plugins>
< plugin>
< artifactId> maven-ejb-plugin< / artifactId>
< version> $ {version.ejb.plugin}< / version>
< configuration>
<! - 告诉Maven我们正在使用EJB 3.1 - >
< ejbVersion> 3.1< / ejbVersion>
< / configuration>
< / plugin>
< / plugins>
< / build>

<个人资料>
<个人资料>
<! - 默认配置文件将跳过所有测试,尽管您可以调整它来运行
只需基于自定义模式的单元测试 - >
<! - 提供分离配置文件用于运行所有测试,包括在指定容器中执行的Arquillian
测试 - >
< id> default< / id>
< activate>
< 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>

<个人资料>
<! - 一个可选的Arquillian测试配置文件,用于在
JBoss EAP实例中执行测试 - >
<! - 此配置文件将启动一个新的JBoss EAP实例,并执行
测试,在完成时将其关闭 - >
<! - 运行:mvn clean test -Parq-jbossas-managed - >
< id> arq-jbossas-managed< / id>
<依赖关系>
<依赖关系>
< groupId> org.jboss.as< / groupId>
< artifactId> jboss-as-arquillian-container-managed< / artifactId>
< scope> test< / scope>
< / dependency>
< / dependencies>
< / profile>

<个人资料>
<! - 一个可选的Arquillian测试配置文件,在远程
中执行测试JBoss EAP实例 - >
<! - 运行:mvn clean test -Parq-jbossas-remote - >
< id> arq-jbossas-remote< / id>
<依赖关系>
<依赖关系>
< groupId> org.jboss.as< / groupId>
< artifactId> jboss-as-arquillian-container-remote< / artifactId>
< scope> test< / scope>
< / dependency>
< / dependencies>
< / profile>

< / profiles>

< / project>


解决方案

我碰到一个类似的问题,容易的解决方法是将枚举定义为公开而不是私有的。



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


I am facing issues while using enum in my J2EE application. I am using enum in a switch case inside my stateless service bean.

During runtime I see following exception on switch statement:

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

This issue has been extensively discussed on one of the thread on SO. But I do not see any solution mentioned to resolve this issue.

In my case I use JBOSS EAP6.1 server. JDK version is 1.7. Code is built using Maven in Eclipse IDE. And application is deployed as EAR archive. How do I add this extra generated class file in classpath inside my EAR archive? Is there any other way to resolve this issue?

Update 29 June 2014: I tried to build application from command line. Then this extra class file is generated. And I am able to deply and execute application successfully. It seems to be bug with eclipse then. Any idea how to resolve it?

pom.xml from EAR project:

<?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>

pom.xml from ejb project:

<?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>

解决方案

I ran into a similar issue, and the easy workaround was to define the Enum as public instead of private.

(Didn't have time to verify, but my hunch is that this causes the class to not be created as Name$1.class but rather something like Name$Enumname.class, which seemed to be the problem)

这篇关于Java:枚举:NoClassDefFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆