类加载器的相关问题(ClassLoader&&LinkageError)

java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing 1 初学spring5时,进行入门测试,手动导入jar包 报出如下错误: java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:763) at java.security.Secur 阅读详情

下面的错误在google上搜了半天也没有特别满意的答案,后来看到一篇关于JUnit类加载器Bug的文章,总算觉得柳暗花明....
  

java.lang.LinkageError: Class org/w3c/dom/Document violates loader constraints at
java.lang.ClassLoader.defineClass0(Native Method) at
java.lang.ClassLoader.defineClass(ClassLoader.java:502) at
java.lang.ClassLoader.defineClass(ClassLoader.java:431) at
.......


这样的异常,主要是由于非系统 类加载器,即自定义的类加载器出现了问题,这个类加载器试图加载org.w3c.dom.Document ,此时如果类加载器没有进行正确的处理,或者Document 类不在JVM's system classpath下,类加载器便无法加载,自己加载不了,也不通知系统类加载器,上面的错误就出现啦。。。

可能理解有问题,可参照下面这篇文章,我试验了一下,完全可信:


Here's a patch for JUnit's busted classloader: junit-patch.jar.  And below is XmlTest.java, a regression test that breaks (with a java.lang.LinkageError) with the old classloader and the swingui/awtui TestRunners, but passes with the patch.

junit-patch.jar contains two classes (source included):

  1. ReloadableTestClassLoader - a properly delegating custom ClassLoader that currently relies on a System property value for its classpath: junit.test.path. The fix can be ported to junit.runner.TestCaseClassLoader or it can be effected through a new ClassLoader like this one.
  2. ReloadingTestSuiteLoader - modified to return this custom ClassLoader. If I can get this fix applied to the real JUnit, this class wouldn't necessarily need to be modified (it's only like 5 lines) because we could fix the original TestCaseClassLoader instead of writing a new one. Whatever; minor detail.

XmlTest.java:

import junit.framework.TestCase;
import javax.xml.parsers.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
    
public class XmlTest extends TestCase {

Document doc = null; public void testXml() throws ParserConfigurationException { doc = DocumentBuilderFactory.newInstance() .newDocumentBuilder() .newDocument(); // System.out.println("Hello! I changed again"); Element root = doc.createElement("xml-test"); assertNotNull(root); } }

This is a simple test that reproduces a LinkageError with the JUnit buggy ClassLoader under the following conditions:

  1. Use Sun JDK 1.4.x
  2. Edit JUnit's excluded.properties so that you comment out the exclusion of org.w3c.dom.* -- this is necessary to reveal the bug and is the easiest test I could think of. Make sure your edited excluded.properties is first in the classpath or updated into junit.jar at junit/runner/excluded.properties (BTW, obviously the new ReloadableTestClassLoader doesn't use or rely on excluded.properties at all).
  3. Put the DOM interfaces (in Xerces these are in xml-apis.jar) and an XML parser impl, such as Xerces, in your classpath to reproduce the bug. Why? Since you will have unexcluded org.w3c.dom.* in excluded.properties, JUnit will attempt to load those interfaces rather than delegate to the system loader. But they aren't in the scope of JUnit's buggy classloader if they aren't in the JVM's system classpath, so you have to add them. I.e., the 1.4.x JVM loads these in its boot loader, whose classpath JUnit's buggy loader doesn't know about.
  4. Example of command line to use to reproduce the bug (using junit.jar from JUnit 3.8.1, but any version should work): java -cp .;junit.jar;xml-apis.jar;xerces.jar junit.swingui.TestRunner XmlTest

You should see this in the runner's error display window:

 java.lang.LinkageError: Class org/w3c/dom/Document violates loader constraints at
java.lang.ClassLoader.defineClass0(Native Method) at 
java.lang.ClassLoader.defineClass(ClassLoader.java:502) at 
java.lang.ClassLoader.defineClass(ClassLoader.java:431) at 
junit.runner.TestCaseClassLoader.loadClass(TestCaseClassLoader.java:104) at 
java.lang.ClassLoader.loadClass(ClassLoader.java:255) at 
java.lang.ClassLoader.loadClassInternal(ClassLoader.java:315) at 
XmlTest.testXml(XmlTest.java:16) at 
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at 
java.lang.reflect.Method.invoke(Method.java:324) at 
junit.framework.TestCase.runTest(TestCase.java:154) at 
junit.framework.TestCase.runBare(TestCase.java:127) at 
junit.framework.TestResult$1.protect(TestResult.java:106) at 
junit.framework.TestResult.runProtected(TestResult.java:124) at 
junit.framework.TestResult.run(TestResult.java:109) at 
junit.framework.TestCase.run(TestCase.java:118) at 
junit.framework.TestSuite.runTest(TestSuite.java:208) at 
junit.framework.TestSuite.run(TestSuite.java:203) at 
junit.swingui.TestRunner$16.run(TestRunner.java:623)

Now run the same test with the fixed ClassLoader:

  1. shadow classes in junit.jar by listing junit-patch.jar first in the classpath
  2. set junit.test.path property to include the path to XmlTest.class
  3. run XmlTest with the Swing or AWT test runner:
    java -cp junit-patch.jar;junit.jar -Djunit.test.path=. junit.swingui.TestRunner XmlTest
FPGA实战:手把手教你用Verilog实现Modbus CRC16校验(附完整代码) 本文详细介绍了在FPGA中实现Modbus RTU协议CRC16校验模块的完整实战方案。针对硬件设计中的时序、资源与性能挑战,文章从算法原理出发,提供了清晰的Verilog模块接口、状态机设计、查表ROM实现及优化策略,并附带了完整的可综合代码与测试平台,帮助工程师高效构建稳定可靠的工业通信系统。 阅读详情

相关推荐

Skynet 框架面试题

本文整理了游戏服务端开发面试中 Skynet 框架的高频考点,涵盖底层架构、消息派发、协程调度、网络 I/O、harbor 跨机通信、内存管理、socket 处理、阻塞操作、错误处理等核心内容。全面覆盖 Skynet 框架的原理、使用和实战问题,追求深入准确,拒绝八股文。

小赖的博客 383

探讨ClassLoader引发的 java.lang.LinkageError

问题产生的原因 我遇到过的多次LinkageError,都是因为调用对象方法时,方法的某部分(参数或者返回值类型)被不同的ClassLoader所加载,下面用几个例子说明。 例子1:web应用 jetty 部署应用的时候,出现了Java LinkageError: java.lang.reflect.InvocationTargetException at sun.reflect.NativeM...

helowken2的博客 3492

基于TI C2000的CCS安装与工程创建完整示例

手把手演示基于TI C2000系列的CCS安装过程及首个工程创建步骤,涵盖环境配置与常见问题处理,助力快速上手嵌入式开发,ccs安装和工程搭建不再难。

weixin_42476987的博客 971

异常java.lang.LinkageError: loader constraints violated when linking

SA 的BOSS系统有后台任务,系统使用Javamail将客户的下月账单发送到客户邮箱中,升级系统后发现后台有报错 java.lang.LinkageError: loader constraints violated when linking

Keep learing, and stay fast 1316

Vue3项目实战:集成h265webjs打造高性能Web端H.265播放器

本文详细介绍了如何在Vue3项目中集成h265webjs库,构建高性能的Web端H.265播放器。通过封装播放器管理模块、利用Vue3组合式API优雅管理生命周期,并提供了从环境准备、核心集成到进阶优化的完整实战指南,有效解决了浏览器原生不支持H.265解码的难题,显著降低视频流带宽占用。

weixin_29016833的博客 364

【Bug已解决】Codex CLI 运行时出现 command not found: codex 解决方案

方案适用场景推荐指数修复 PATH 配置最常见的基础排查方向⭐⭐⭐⭐⭐检查原生二进制完整性PATH 正常但执行仍然异常⭐⭐⭐⭐统一包管理器混用多种包管理器导致混乱⭐⭐⭐⭐确认安装脚本未被跳过CI/CD 或企业策略环境⭐⭐⭐⭐排查网络下载渠道企业内网/受限网络环境⭐⭐⭐需要留意 warning 中是否涉及脚本执行失败、网络超时重试等信息,这类 warning 往往是后续问题的前兆,建议出现明显的下载/网络相关警告时就主动重新安装一次确认。Codex CLI 报。

一键难忘的博客 1万+

利用Loadrunner进行API压测,解决脚本出现:Error: at java.lang.ClassLoader.defineClass1(Native Method)问题过程及结果

出现错误的信息: Error: at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631) at java.lang.ClassLoader.defineClass(ClassLoader.java:615) at j...

爱举铁的Tester 1万+

杰理AC695N芯片跑iic接口的oled驱动分享

本文介绍了一个基于IIC接口的0.96寸OLED显示屏驱动程序,适用于SSD1306控制器。驱动采用软件模拟IIC方式,支持字符、数字、字符串、汉字和图片显示,测试平台为杰理AC695N系列芯片。硬件参数包括128×64分辨率,IIC接口(地址0x78),使用IO_PORTA_02(SDA)和IO_PORTA_03(SCL)引脚。驱动包含OLED主文件(oled.c/h)、软件IIC实现(soft_iic.c/h)和字体/图片数据文件(oledfont.h/bmp.h),可通过修改soft_iic.h中的引

qq_42014563的博客 882

已解决java.lang.LinkageError: 链接错误的正确解决方法,亲测有效!!!

已解决java.lang.LinkageError: 链接错误的正确解决方法,亲测有效!!!

小明的Java问道之路 2375

YOLOv8首创原创改进:即插即用|创新的轻量化非对称多级压缩LADH检测头

总结而言,本文介绍了一种名为轻量化非对称多级压缩LADH检测头的原创改进,该改进可与YOLOv8无缝适配,并显著提升了检测器的性能。在本文中,我们介绍了一种原创改进的检测头,名为轻量化非对称多级压缩LADH检测头,它可与YOLOv8无缝适配,显著提升了检测器的性能。此外,我们引入了多级压缩的概念,通过在不同的检测层级上应用不同程度的压缩,进一步提升了检测头的轻量化特性。具体而言,我们采用了逐级减少卷积核数量和通道数量的策略,以降低模型的参数量和计算复杂度,同时保持较高的检测精度。

IbcVue的博客 2182

AgentScope Java 2.0 项目实战: 从零构建自己的个人AI助手“小龙虾“

本文系统介绍如何基于Spring Boot WebFlux、React 18和TypeScript,从零搭建一个本地化的个人AI助手管理平台——AgentScope Paw。项目支持多Agent创建与管理、SSE流式对话、工作区文件CRUD、技能市场、子智能体孵化、AI辅助起草Agent配置等核心功能,并深度集成钉钉、企业微信、飞书、GitHub等IM通道。

program哲学 1430

加载主类 Hello 时出现 LinkageError java.lang.UnsupportedClassVersionError

JDK17和eclipse 2021-09版本 运行时报错: 加载主类 Hello 时出现 LinkageError java.lang.UnsupportedClassVersionError: Hello (class file version 60.65535) was compiled with preview features that are unsupported. This version of the Java Runtime only recognizes preview featur

qq_43711051的博客 4610

C/C++跳动的爱心(完整代码)

C语言实现跳动的爱心的完整代码。

AIGC 5843

javax.servlet.ServletException: Class javax/el/ValueExpression violates loader constraints

调试公司项目时,修改了jsp文件,再次访问就提示标题的错误,但是重启服务后,访问就正常,影响效率,网上搜了很多资料,大多都是以下的说法: 我用的容器是jetty,并没搜到el-api,查看错误内容,猜测可能是ValueExpression这个java类冲突,项目里搜该方法, 发现在jsp-api-2.1.jar这个jar包里有该方法。 于是项目里搜,共搜到两个jsp-api-2.1.ja...

haoyulinwen的专栏 422

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

Eclipse报错(一)【错误: 加载主类 day007.Hello 时出现 LinkageError】

错误: 加载主类 day007.Hello 时出现 LinkageError java.lang.UnsupportedClassVersionError: day007/Hello has been compiled by a more recent version of the Java Runtime (class file version 60.0), this version of the Java Runtime only recognizes class file versions up t.

牢骚太盛防肠断,风物长宜放眼量 3741

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

1、加载主类时出现 LinkageError

加载主类 com.itheima.hello.HelloWorld 时出现 LinkageError原因:项目jdk版本与IDEA的jdk版本不一致解决方法:点击左下角扳手按钮设置项目jdk的版本再进入Project Structure查看idea的jdk版本。将两个jdk版本设置成相同的即可解决。...

飞翔的企鹅的博客 1万+

罕见类加载冲突问题:LinkageError

问题描述假设有C1类和C2类都依赖C0,C1和C2分别用不同的2个类加载器加载,而这两个类加载器都能在自己的类加载路径中加载到C0,这个时候如果在C1中调用C2的某个方法(注:这个方法的签名中依赖了C0)就会出现LinkageError错误。用例模拟及分析冲突的依赖类,模拟问题描述中的C0package loader;public class ConflictDependence { } 发生错误的

王鹏亮 的专栏 6907

Eclipse出现"错误: 找不到或无法加载主类"的情况

在使用eclipse时遇到了“错误: 找不到或无法加载主类”的问题,经过多次尝试和排查终于解决了。

Hello World 1万+

eclipse 找不到或无法加载主类(已解决)

eclipse 找不到或无法加载主类错误解决方法

m0_49373319的博客 6206

eclipse错误: 找不到或无法加载主类 Test

在用Eclipse进行编译java程序的时候,突然有一天出现了以下问题: 错误: 找不到或无法加载主类 Test 但是用cmd命令运行则没有问题 上网找了下,发现原来是eclipse的jdk配置出了问题,前几天将1.8版本的jdk卸载了安装了1.7的,这就是问题所在 在所要运行的Main方法中右键选择Run As,后选择Run Configurations,进行jre版本配置 配置后

cc的博客 2万+

错误: 加载主类时出现 LinkageError,java.lang.UnsupportedClassVersionError 解决方案

报错的解决办法记录

zhuyalan12的博客 8158

LinkageError之loader (instance of xxx) previously initiated loading for a different type with name "l...

LinkageError包括其子类,是Java中比较不应该出现的Error。出现这些问题,大概有几个问题:ClassLoader没有严格遵守Java中默认的双亲委派模式;全限定名相同的两个类在不同的CL中有重复;程序运行时使用的类的版本与开发时候不一样(类有变化,比如改了方法的可见性等)。 而LinkageError本身则更少见。当遇到LinkageError loader (inst...

weixin_30642029的博客 947

对LinkageError的分析

同事开发时遇到些类加载的问题,在协助他fix的时候,自己也对类加载相关知识有了更深的理解。现在回顾总结下。 环境:运行在OSGI环境中 代码如下(需注意:我们自定义的ReviceContractPayInfo_Service 继承了rt.jar中javax.xml.ws.Service这个类):import java.net.MalformedURLException; import ja

1406
上一篇: ThreadLocal的相关资料
differ1
博客等级 码龄25年 0粉丝 25原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值