JBoss的7和java.lang.NoClassDefFoundError:com/sun/image/codec/jpeg/JPEGCodec

本文详细介绍了在使用JBoss服务器部署应用时遇到的Java.lang.NoClassDefFoundError问题,特别是与com/sun/image/codec/jpeg/JPEGCodec类相关的问题。文章提供了解决方案,包括修改模块XML文件来引入缺失的类路径,从而解决运行时错误。通过此解决方案,开发者可以避免在多环境切换或部署到新服务器时遇到此类问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

验证码在本地环境显示正常,上传到jboss服务器后报错java.lang.NoClassDefFoundError:com/sun/image/codec/jpeg/JPEGCodec

参考一篇文章:http://www.derrickwilliams.com/?p=56

So you’re tooling along and make a change to your development environment or deploy to a new server, then you suddenly hit on a stacktrace like this:

Caused by: java.lang.NoClassDefFoundError: com/sun/image/codec/jpeg/JPEGCodec
        at com.aspose.pdf.kit.n.a(Unknown Source) [aspose-pdf-kit-4.3.0.jar:]
        at com.aspose.pdf.kit.PdfConverter.doConvert(Unknown Source) [aspose-pdf-kit-4.3.0.jar:]
        at com.ity.ityweb.util.FileUploadController.copyFile(FileUploadController.java:77) [ITYWeb-ejb.jar:]
        at com.ity.ityweb.util.FileUploadController.handleFileUpload(FileUploadController.java:41) [ITYWeb-ejb.jar:]
        ... 67 more
Caused by: java.lang.ClassNotFoundException: com.sun.image.codec.jpeg.JPEGCodec from [Module "deployment.ITYWeb-ear.ear:main" from Service Module Loader]
        at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:190)
        at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:468)
        at org.jboss.modules.ConcurrentClassLoader.performLoadClassChecked(ConcurrentClassLoader.java:456)
        at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:398)
        at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:120)
        ... 71 more

 

Changing your runtime environment (to Java 7, for example) is a good way to cause this. The basic explanation is that com.sun.image.codec.jpeg.JPEGCodec and related libraries have long been marked as depreciated, and you should use the ImageIO library instead. Cold comfort if you are using libraries that you have no control over and can’t do the conversion and re-build them. The quick way to resolve this runtime error in JBoss is to put an entry in the <JBoss-install-diectory>/modules/sun/jdk/main/module.xml file:

<module xmlns="urn:jboss:module:1.1" name="sun.jdk">
    <resources>
        <!-- currently jboss modules has not way of importing services from
        classes.jar so we duplicate them here -->
        <resource-root path="service-loader-resources"/>
    </resources>
    <dependencies>
        <system export="true">
            <paths>
                <path name="com/sun/script/javascript"/>
                <path name="com/sun/jndi/dns"/>
                <path name="com/sun/jndi/ldap"/>
                <path name="com/sun/jndi/url"/>
                <path name="com/sun/jndi/url/dns"/>
                <path name="com/sun/security/auth"/>
                <path name="com/sun/security/auth/login"/>
                <path name="com/sun/security/auth/module"/>
                <path name="sun/misc"/>
                <path name="sun/io"/>
                <path name="sun/nio"/>
                <path name="sun/nio/ch"/>
                <path name="sun/security"/>
                <path name="sun/security/krb5"/>
                <path name="sun/util"/>
                <path name="sun/util/calendar"/>
                <path name="sun/util/locale"/>
                <path name="sun/security/provider"/>
                <path name="META-INF/services"/>
                <path name="com/sun/image/codec/jpeg"/>
            </paths>
            <exports>
                <include-set>
                    <path name="META-INF/services"/>
                </include-set>
            </exports>
        </system>
    </dependencies>
</module>

Note the line path name=”com/sun/image/codec/jpeg” added to the <paths> subtree.

Your stacktrace regarding the com/sun/image/codec/jpeg/JPEGCodec error will be resolved and you will go on your merry way in your deployment.

`java.lang.NoClassDefFoundError: org/jboss/netty/channel/AdaptiveReceiveBufferSizePredictor` 是在运行 Java 应用程序时常见的错误之一,表示 JVM 在运行时找不到指定的类。此类错误通常与类路径(classpath)配置不正确或依赖库缺失有关。 ### 原因分析 1. **Netty 依赖缺失** `org.jboss.netty.channel.AdaptiveReceiveBufferSizePredictor` 是 Netty 3.x 版本中的类,用于动态调整接收缓冲区大小。如果项目依赖的 Netty 库未正确引入或版本不兼容,就会导致此类错误[^1]。 2. **依赖版本冲突** 如果项目中同时存在多个版本的 Netty 库,可能会导致类加载冲突。例如,某些库可能默认引入了较旧版本的 Netty,而项目本身需要更高版本的功能。 3. **依赖作用域或打包问题** 如果使用 Maven 或 Gradle 等构建工具,但依赖作用域设置错误(如 `provided` 或 `test`),或者未将依赖打包进最终的 JAR/WAR 文件中,也会导致运行时类缺失。 --- ### 解决方案 #### 1. 确保引入正确的 Netty 依赖 如果项目使用 Maven,可以在 `pom.xml` 中添加以下依赖以引入 Netty 3.x: ```xml <dependency> <groupId>org.jboss.netty</groupId> <artifactId>netty</artifactId> <version>3.2.10.Final</version> </dependency> ``` 如果是 Gradle 项目,可在 `build.gradle` 中添加: ```groovy implementation 'org.jboss.netty:netty:3.2.10.Final' ``` 确保依赖版本与代码兼容,尤其是如果使用了特定版本的功能或类。 #### 2. 检查依赖冲突 使用 `mvn dependency:tree`(Maven)或 `gradle dependencies`(Gradle)命令查看依赖树,确认是否有多个 Netty 版本被引入。如果有冲突,可以通过 `exclusion` 排除不需要的版本: ```xml <dependency> <groupId>some.library</groupId> <artifactId>some-artifact</artifactId> <exclusions> <exclusion> <groupId>org.jboss.netty</groupId> <artifactId>netty</artifactId> </exclusion> </exclusions> </dependency> ``` #### 3. 检查构建输出 确保构建工具将 Netty 的 JAR 包正确打包进最终的可执行文件中。例如,在 Maven 中使用 `maven-assembly-plugin` 或 `maven-shade-plugin` 来打包所有依赖: ```xml <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <executions> <execution> <phase>package</phase> <goals><goal>shade</goal></goals> <configuration> <transformers> <transformer implementation="..."> <mainClass>com.example.Main</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> ``` #### 4. 手动添加 JAR 包 如果无法通过构建工具管理依赖,可以手动下载 Netty 的 JAR 文件并将其加入项目的构建路径中。从 [Maven Central](https://search.maven.org/artifact/org.jboss.netty/netty) 下载合适的版本,然后将其复制到项目的 `lib` 目录,并在 IDE 或构建脚本中添加引用。 --- ### 验证解决 完成上述步骤后,重新启动应用程序并观察是否仍然出现 `NoClassDefFoundError`。如果问题依旧,建议检查运行时类路径是否包含 Netty 的 JAR 文件,或者尝试升级到 Netty 4.x(注意 API 已发生重大变化)。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值