java class loader

Android解析ClassLoader(一)Java中的ClassLoader 热修复和插件化是目前比较热门的技术,要想更好的掌握它们需要了解ClassLoader,因此也就有了本系列的产生,这一篇我们先来学习Java中的ClassLoader。 阅读详情

Problem

In a project, We use a stub jar file generated by Axis2. It always throw: java.lang.NoSuchFieldError: localClientNameTracker

 

While in another project, everything is ok using same jar file. No exception is thrown.

 

CLUE

  1. Guess it's because environment, but has no idea about the cause. 
  2. Clean the project and re-add all the lib in Eclipse, the issue disappear 
  3. If we put axis1 before axis2 on classpath, the issue disappear but the project broken
  4. If we put axis2 before axis1 on classpath, the project ok but the issue occur

 

Suspect

 

It seem because of Axis1 and axis2 jar file conflict,  we need dig into detail to find out which jar cause this conflict.

 

 

diagnosis 

  1. Search the internet, try to find the Axis1 and Axis2 jar conflict, do not any any hit
  2. Search the internet, try to understanding class loader
  •  
    • prints a trace of the class loading process on the console in Eclipse . From an article on IBM java programming dynamics series, part1 Java classes and class loadin g.
    • In Eclipse, we append Eclipse console to the File , Run Configurations -> Common -> Standard Input and Output
    • Use Beyond compare to compare the two trace file, one is success trace, the other is fail trace

Cause

 

jar dependencies

  1. localClientNameTracker is a field in a parent class called ServerReuqest (x.jar)
  2. There is happened another jar file(y.jar) which has same class with exactly same class name and package name, while they are different version.
  3. When add y.jar file before x.jar into classpath, then JVM search y.jar first and load ServerRequest from y.jar file. Then NoSuchFieldError occur 
  4. When add x.jar file before y.jar into classpath, then JVM search x.jar first and load ServerRequest from x.jar file, everything is OK.

Solution1

According to problem solving policy : Try to find another ways which achieve same target but do not this problem. So our first solution is remove two version of same classes. Only keep a latest version of ServerRequest

jar_dependencies_2

 

 

Solution2

How about keep two version of same jar file.  According to Managing Component Dependencies Using ClassLoaders

We can let each jar file have one classloader, see the diagram 

 

class loader

The policy of our class loader

  1. Each Jar file has own class loader, JarClassLoader, which extended from URLClassLoader and load class from a single jar file. If can not load from jar, delegate to AppJarClassLoader.
  2. Add a Customer class loader (AppJarClassLoader ) to boot class path as System class loader, which iterate each JarClassLoader. If can not load from jars, delegate to the parent.    
  3.  If no parent delegation, NoClassDefError may be occur.

 

 

 

postmortem

ClassLoader

 

  • Startup (Main class is loaded by bootstrap class loader, In Sun JVM, Default loaded by AppClassLoader)

The Java virtual machine starts up by creating an initial class, which is specified in an implementation-dependent manner, using the bootstrap class loader (§5.3.1) . The Java virtual machine then links the initial class, initializes it, and invokes its public  class method void  main(String[]) . The invocation of this method drives all further execution. Execution of the Java virtual machine instructions constituting themain  method may cause linking (and consequently creation) of additional classes and interfaces, as well as invocation of additional methods.

At run time, a class or interface is determined not by its name alone, but by a pair: its fully qualified name and its defining class loader. Each such class or interface belongs to a single runtime package . The runtime package of a class or interface is determined by the package name and defining class loader of the class or interface.

 

  • Same Loader (L load c, if c reference d, then L will load d)

Creation of a class or interface C denoted by the name N consists of the construction in the method area of the Java virtual machine (§3.5.4)  of an 

implementation-specific internal representation of C. Class or interface creation is triggered by another class or interface D, which references C through its runtime constant pool. Class or interface creation may also be triggered by D invoking methods in certain Java class libraries (§3.12)  such as reflection.

 

The Java virtual machine uses one of three procedures to create class or interface C denoted by N:

If N denotes a nonarray class or an interface, one of the two following methods is used to load and thereby create C :

If D was defined by the bootstrap class loader, then the bootstrap class loader initiates loading of C (§5.3.1).

If D was defined by a user-defined class loader, then that same user-defined class loader initiates loading of C (§5.3.2).

Otherwise N denotes an array class. An array class is created directly by the Java virtual machine (§5.3.3), not by a class loader. However, the defining class loader of D is used in the process of creating array class C.

 

ClassNotFoundException NoClassDefFoundError

Basic Class Loading problems

 

ClassNotFoundException  but no definition for the class with the specified name could be found.

NoClassDefFoundError  is another common exception thrown by the class loader during the loading phase. The JVM specification defines NoClassDefFoundError  as follows

Essentially, this means that a NoClassDefFoundError  is thrown as a result of a unsuccessful implicit class load.

Java

-verbose:class
Display information about each class loaded.
-verbose:gc
Report on each garbage collection event.
-verbose:jni
Report information about use of native methods and other Java Native Interface activity.
-Xprof
Profiles the running program, and sends profiling data to standard output. This option is provided as a utility that is useful in program development and is not intended to be used in production systems.

-Xbootclasspath: bootclasspath
Specify a semicolon-separated list of directories, JAR archives, and ZIP archives to search for boot class files. These are used in place of the boot class files included in the Java 2 SDK.  Note: Applications that use this option for the purpose of overriding a class in rt.jar should not be deployed as doing so would contravene the Java 2 Runtime Environment binary code license.
-Xbootclasspath/a: path
Specify a semicolon-separated path of directires, JAR archives, and ZIP archives to append to the default bootstrap class path.
-Xbootclasspath/p: path
Specify a semicolon-separated path of directires, JAR archives, and ZIP archives to prepend in front of the default bootstrap class path.  Note: Applications that use this option for the purpose of overriding a class in rt.jar should not be deployed as doing so would contravene the Java 2 Runtime Environment binary code license.



 

Tomcat Classloader

 

Finding a way out of ClassLoader , the author said we have at least 3 choices:

the  system   (also referred to as the  application ) classloader, the  current   classloader, and the current thread  context   classloader. The question above refers to the latter. Which classloader is the right one?

If you choose the system classloader

  As soon as you move your code into an Enterprise JavaBean, a Web application, or a Java Web Start application, things are guaranteed to break.

So let's digg into Tomcat to see how a web concainter using customized class loader.

    

 
So each webapp has own class loader. Tomcat implements it by controlling the App entries: Filter, Listener, Servlet Listener Each StandardContext represent an App
  Filter

Each StandardWrapper is an individual servlet definition. StandardWrapperValue is a pipeline implements the default basic behavior for the StandardWrapper

 

Servlet

 

WebAppClassLoader

It's very important in web class loader, it verfiy the jar file, if it's servlet jar, discard this jar file. So all the interface javax.servlet is loaded from parent class loader while all the implementation is loaded from App class loader

 

OSGI

We do not need define own class loader, Other choice is OSGI

http://www.slideshare.net/stephanj/intro-to-osgi

 

一看你就懂,超详细java中的ClassLoader详解 本篇文章已授权微信公众号 guolin_blog (郭霖)独家发布 ClassLoader翻译过来就是类加载器,普通的java开发者其实用到的不多,但对于某些框架开发者来说却非常常见。理解ClassLoader的加载机制,也有利于我们编写出更高效的代码。ClassLoader的具体作用就是将class文件加载到jvm虚拟机中去,程序就可以正确运行了。但是,jvm启动的时候,并不会一次性加载所有的... 阅读详情

相关推荐

从程序装载到动态实例化,ABAP 与 Java Class Loader 的真正对应关系

文章摘要: ABAP与Java的类加载机制存在本质区别。虽然ABAP支持动态创建对象(CREATE OBJECT TYPE (class_name)),但并未开放类似Java Class Loader的自定义类加载能力。Java Class Loader允许从多种来源加载类、建立层级关系,并支持同名类的多版本共存。而ABAP的类加载由平台内部管理,依赖中央Repository,业务代码无法控制加载过程。ABAP通过组合动态对象创建、RTTI和依赖注入等机制实现类似功能,但仍不具备Java Class Loa

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 30

Java中类ClassLoader(加载器)全解

Java中类ClassLoader(加载器)全解。引导类加载器、扩展类加载器、系统类加载器负责加载范围详解;ClassLoader源码解析;双亲委派模型流程详解、优势和劣势详解。

全栈行动派的博客 1万+

jarinjarload例子和源代码

jarinjarload例子和源代码

什么是JAVA的ClassLoader

一、楔子 在读取Jar包中的资源文件时,一般会使用this.getClass().getResourceAsStream方法,使用ClassLoader以流的形式来读取资源文件,那究竟什么是ClassLoader?本文就来一探究竟。 二、什么是ClassLoader? ClassLoader是一个抽象类,我们用它的实例对象来装载类 (Java默认提供的三个ClassLoader),它负责将 Jav...

借你一秒 1517

风光储互补微电网在偏远地区的应用实践:从建模到仿真全解析

本文深入解析了风光储互补微电网在偏远地区的应用实践,从系统设计到仿真建模全流程。通过Simulink等工具进行动态仿真,验证了系统在多种工况下的可靠性,并结合实际案例展示了其显著的经济和环保效益。文章为从业者提供了从技术路径到实施优化的全面指导。

weixin_29047545的博客 271

腾讯大佬总结的170道 python面试题

简述基于 TCP 协议的套接字通信流程。19.面向对象中`__new__` 和 `__init__` 区别。86. 解释 Python 中的`//`,`%`和`**`运算符。45.isinstance 的作用以及与 type()的区别。3. 简述下 Python 中的字符串、列表、元组和字典。143. 如何高效的找到 redis 中的某个 KEY。140. 如何保证 redis 中的数据都是热点数据。147. redis 中的 watch 的命令的作用。108. 简述 进程、线程、协程的区别以及应用场景。

AudiA6LV6的博客 491

Java类加载器-ClassLoader

1、ClassLoader用来加载class字节码文件,生成类对象。 2、系统内置的ClassLoader通过双亲委托机制来加载指定路径下的class和资源。 3、ContextClassLoader与线程相关,可以获取和设置类加载器。 4、灵活运行自定义ClassLoader,可以解决类冲突问题,实现热加载以及热部署,甚至可以实现jar包的加密保护。

weixin_40017062的博客 2077

避坑指南:Abaqus网格划分中90%用户会遇到的5个质量陷阱(附解决方案)

本文深入剖析Abaqus网格划分中90%用户会遇到的5个典型质量陷阱,包括单元扭曲、扫掠网格失败、尺寸梯度控制等,并提供工程级解决方案。通过Python脚本和虚拟拓扑等技巧,帮助用户提升网格质量,确保有限元分析结果的可靠性,适用于CAE工程师和仿真分析师。

weixin_29243279的博客 103

Java中的Class Loader

Java的ClassLoader Java中的ClassLoader采用Delegation机制。即每一个ClassLoader都有自己的Parent Class Loader,当从一个Class Loader中加载一个Class时,会先到当前ClassLoader的Parent中寻找该Class是否已经被加载,如果是,则从其Parent Class Loader中得到Class的Instan

duqing72的专栏 1707

C#连接数据库报Authentication to host ‘127.0.0.1‘ for user ‘root‘ using method ‘caching_sha2_password‘ fail

所以导致的,除了上边的办法,还可以把这个加密规则修改一下,这个的修改方式有很多,具体原因是因为MySQL8.0以前的加密规则是。

weixin_42209881的博客 4131

Android SystemUI——自定义状态栏和导航栏(十二)

通过前面的文章内容,我们了解了Android系统原生的状态栏StatusBar和车载系统状态栏CarStatusBar的启动流程以及视图构建流程,这里我们来简单的看一下自定义状态栏和导航栏视图的实现流程。

小旭的专栏 2013

解析Java Class Loader 类加载过程

Java基本结构Java结构包括四个不同而又相关的部分:Java语言、class文件格式、应用程序接口、虚拟机其中,虚拟机的主要任务是在程序运行中需要的时候调用class文件并执行其中的字节码。一个Java应用有两种class loader:the system class loader 和 class loader objects。The primordial class loader 通常装载

Enjoy Life with Sun 2451

霜冰优化算法(RIME)详解:从自然现象到智能优化

霜冰优化算法(RIME)是一种新型元启发式优化算法,灵感源于自然界中霜冰的形成过程。该算法通过模拟软霜和硬霜的不同生长特性,实现了高效的全局探索和局部开发平衡。RIME算法包含三个核心机制:软霜搜索策略模拟随机覆盖特性实现全局探索;硬霜穿刺机制模拟定向生长特性进行局部开发;正向贪婪选择机制确保解的持续优化。与遗传算法、粒子群算法相比,RIME具有参数少、收敛快、自适应平衡等优势,特别适合处理高维复杂优化问题。算法已在工程优化、机器学习等领域获得广泛应用,发表一年内即成为高被引论文。

qq_42212808的博客 787

class jdk.internal.loader.ClassLoaders$AppClassLoader cannot be cast to class java.net.URLClassLoade

class jdk.internal.loader.ClassLoadersAppClassLoadercannotbecasttoclassjava.net.URLClassLoader(jdk.internal.loader.ClassLoadersAppClassLoader cannot be cast to class java.net.URLClassLoader (jdk.internal.loader.ClassLoadersAppClassLoadercannotbecasttoclass

梦里逆天的博客 1万+

樽海鞘优化算法详解:原理、实现与应用

本文详细介绍了樽海鞘优化算法(SSA)的原理、实现与应用。该算法模拟樽海鞘链式群体行为,将优化问题解空间映射为群体移动空间,通过领导者引导和追随者跟随的协作机制寻找最优解。文章从生物特性、数学模型到Python代码实现进行了系统阐述,包括种群初始化、位置更新公式和收敛因子等核心要素,并展示了算法在函数优化等领域的应用潜力。该算法具有结构简单、参数少、实现容易等特点,在多种优化问题上表现良好。

qq_42212808的博客 647

端侧 AI 落地避坑指南(中):为什么 QAT 救不了精度?——对齐“融合后算子“的硬件物理约束

在上一篇文章中,我们深入探讨了量化的本质,并得出结论:QAT(量化感知训练)是保持精度的关键。明明做了 QAT,精度还是崩塌了!让我们看一个真实的案例:✅ 使用了✅ 配置了量化✅ 训练了 50 个 epoch,学习率降为原来的 1/10✅ 冻结了 BN 统计量FP32 模型:mAP = 0.85QAT 后 FP32:mAP = 0.84(正常)工具量化后:mAP = 0.68(崩塌!问题出在哪里?层融合(Layer Fusion)机制。

weixin_51988935的博客 57

Java Class Loader

Java中有三种ClassLoader   1. Bootstrap class loader:     用于加载java语言规范所定义的标准库的类,比如String,ArrayList,Object等。        它通常是在虚拟机种用本地代码(如C)实现,在系统中用null表示。     这里之所以返回null,与其说是技术原因,到不如说更多得是出于安全考虑。     试想,如...

SAP => Simplify Anything Possible 196

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

JAVA Class Loader

import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import

SHUAI-优快云 323

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

java class Loader

当一个类装载器(class loader)被请求装载类时,它首先按照顺序在上层装载器、父装载器以及自身的装载器的缓存里检查这个类是否已经存在。简单来说,就是在缓存里查看这个类是否已经被自己装载过了,如果没有的话,继续查找父类的缓存,直到在bootstrap类装载器里也没有找到的话,它就会自己在文件系统里去查找并且加载这个类。 启动类加载器(Bootstrap class loader):

yang1370808的博客 405

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

JAVA CLASS LOADER

出处:http://www.iteye.com/topic/1005717 1. ClassLoader    类加载器(class loader)用来加载 Java 类到 Java 虚拟机中。Java 源程序(.java 文件)在经过 Java 编译器编译之后就被转换成 J

技术博客 689

【java class loader】

前言 classloader的主要职责就是负责加载各种class文件到jvm中,classloader是一个抽象的class,给定一个class的二进制文件名,classloader会尝试加载并且在JVM中生成构成这个类的各个数据结构,然后使其分布在JVM对应的内存区域中. 1.类的加载过程简介 分为三个比较大的阶段,分别是加载阶段,连接阶段和初始化阶段. 加载阶段:主要负责查找并且加...

展翅的雄鹰 463

Java class loader的一些总结

1.  classloader.loadclass 与 class.forName 之间的区别 http://stackoverflow.com/questions/4285855/difference-betweeen-loading-a-class-using-classloader-and-class-forname http://www.javaworld.com/javaworld/

行风 1694

Java class loader

[list] [*]1. Explain Java class loader? [/list] Class loader are hierarchical and use a delegation model when loading a class. Here is the “Typical Default Class Loader Hierarchy”. [img]ht...

d02540315 186
上一篇: Integer.toHexString cannot be decoded back Integer.parseInt
下一篇: Programmer Competency Matrix
FireCoder
博客等级 码龄17年 65粉丝 100原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FireCoder

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值