ClassLoader专题

【AOSP专题】07. FART脱壳-02 摘要 本文介绍了Android应用加固和脱壳的相关技术,主要包含以下内容: 加固技术:通过抽取和加密应用中的方法,仅在执行时临时解密加载到内存,执行完毕后再次被抽取,以增强安全性。 脱壳原理:通过主动调用类中的所有方法,将代码加载至内存后导出,包括: 寻找所有ClassLoader 根据ClassLoader定位dex文件 使用反射机制提取类成员并导出 ClassLoader机制:详细讲解了Android中的双亲委派模型,包括BootClassLoader、PathClassLoader和DexClassL 阅读详情

 

(一):ClassLoader基础

JVM启动,会形成3个类加载器组成的初始化加载器层次结构:
bootstap classloader (加载核心类)
        ||
extension classloader(加载ext(目录),即java.ext.dirs())
        ||
system classloader   (加载-classpath或者java.class.path或者CLASSPATH)


ClassLoader机制:
a)全盘负责:一个classloader加载一个class后,这个class所引用或者依赖的类也由这个classloader载入,除非显示的用另一个classloader载入
b)委托机制:先由父加载器加载,除非父加载器找不到时才从自己的类路径中去寻找
c)Cache机制:classloader采用缓存机制,即先查cache;若cache中保存了这个class就直接返回;若无,才从文件读取和转化为class并放入cache

 

ClassLoader加载类顺序:
1)检查cache是否有该类:
    11)若有直接返回
    12)若无,请求父类加载
        121) 若无父,则从bootstap classloader加载
2)加载:
    21)寻找class文件(丛与此classloader相关的类路径中寻找)
    22)从文件载入class
    23)找不到则抛出ClassNotFoundeException
3)扩展:
    记载时即2),覆写findClass可以实现自己的载入策略
    记载时即2),覆写loadClass来实现自己的载入过程


如何实现运行时动态载入与更新
本质:只要动态改类搜索路径和清除classloader的cache已载入的class就ok
做法:
1)继承ClassLoader:覆写loadClass方法,动态寻找class文件
2)只要重新使用一个新的类搜索路径来new一个classloader就可以,这样既更新了类的搜索路径以便来载入新的class,也更新生成了一个空白的cache


classloader载入的方式
1)Pre-loading 预先载入,载入基础类
2)load-on-demand 按需求载入


JDK为啥有两个JRE?
JDK中jre是运行java本身的程序,如javac
ProgramFile(默认安装)中jre是运行用户编写的java程序

(2) 从 Servlet 容器看 ClassLoader 机制的妙用

classloader有啥妙用(1)?
这个问题得从自定义的classloader身上说,那自定义classloader缘由是什么呢?
告诉你:大多是因为编译时无法预知运行时需要哪些类,特别是app server;因此自定义classloader,运行时指定路径,来加载这个路径下的class


特殊说明
特殊说明1:如果没有特殊指定,用户自定义的classloader都把system classloader作为它的父加载器
特殊说明2:jvm认为不同的classloade载入相同名字的class是不同的,即使从同一个class文件载入


classloader有啥妙用(2)?
看到特殊说明2,你或许就会感觉疑惑或者不爽;啥概念?
以servlet、ejb等容器来剖析这个问题:

将接口或者基类放入classpath                         <---------system classloader
执行时,动态载入实现或者继承这些接口或者基类的子类;<---------customized classloader
         ||
         ||
用customized classloader载入类时,发现它有一个父类class(extends);
但是在载入它时,jvm先加载父类class; 这个父类是system classloader能识别的; 根据“委托机制”它将由system classloader来加载;
然后customized classloader(实际是system classloader来加载)再载入这个class,创建一个实例,转型为父类;
jvm就使用system classloader再次载入父类class,然后将此实例转型为这个父类class;

这个过程加载了两个父类class,都是由system classloader载入;即同一个classloader载入同一个文件,造型不会由异常

web app server大概是这样工作的;这样载入了任何继承了servlet的class并正确运行它们,不管class是什么,都它们实例化为一个servlet class.

何时使用Thread.getContextClassLoader()?

    这是一个很常见的问题,但答案却很难回答。这个问题通常在需要动态加载类和资源的系统编程时会遇到。总的说来动态加载资源时,往往需要从三种类加载器里选择:系统或说程序的类加载器、当前类加载器、以及当前线程的上下文类加载器。在程序中应该使用何种类加载器呢?

    系统类加载器通常不会使用。此类加载器处理启动应用程序时classpath指定的类,可以通过ClassLoader.getSystemClassLoader()来获得。所有的ClassLoader.getSystemXXX()接口也是通过这个类加载器加载的。一般不要显式调用这些方法,应该让其他类加载器代理到系统类加载器上。由于系统类加载器是JVM最后创建的类加载器,这样代码只会适应于简单命令行启动的程序。一旦代码移植到EJB、Web应用或者Java Web Start应用程序中,程序肯定不能正确执行。

    因此一般只有两种选择,当前类加载器和线程上下文类加载器。当前类加载器是指当前方法所在类的加载器。这个类加载器是运行时类解析使用的加载器,Class.forName(String)和Class.getResource(String)也使用该类加载器。代码中X.class的写法使用的类加载器也是这个类加载器。

    线程上下文类加载器在Java 2(J2SE)时引入。每个线程都有一个关联的上下文类加载器。如果你使用new Thread()方式生成新的线程,新线程将继承其父线程的上下文类加载器。如果程序对线程上下文类加载器没有任何改动的话,程序中所有的线程将都使用系统类加载器作为上下文类加载器。Web应用和Java企业级应用中,应用服务器经常要使用复杂的类加载器结构来实现JNDI(Java命名和目录接口)、线程池、组件热部署等功能,因此理解这一点尤其重要。

    为什么要引入线程的上下文类加载器?将它引入J2SE并不是纯粹的噱头,由于Sun没有提供充分的文档解释说明这一点,这使许多开发者很糊涂。实际上,上下文类加载器为同样在J2SE中引入的类加载代理机制提供了后门。通常JVM中的类加载器是按照层次结构组织的,目的是每个类加载器(除了启动整个JVM的原初类加载器)都有一个父类加载器。当类加载请求到来时,类加载器通常首先将请求代理给父类加载器。只有当父类加载器失败后,它才试图按照自己的算法查找并定义当前类。

    有时这种模式并不能总是奏效。这通常发生在JVM核心代码必须动态加载由应用程序动态提供的资源时。拿JNDI为例,它的核心是由JRE核心类(rt.jar)实现的。但这些核心JNDI类必须能加载由第三方厂商提供的JNDI实现。这种情况下调用父类加载器(原初类加载器)来加载只有其子类加载器可见的类,这种代理机制就会失效。解决办法就是让核心JNDI类使用线程上下文类加载器,从而有效的打通类加载器层次结构,逆着代理机制的方向使用类加载器。

    顺便提一下,XML解析API(JAXP)也是使用此种机制。当JAXP还是J2SE扩展时,XML解析器使用当前类加载器方法来加载解析器实现。但当JAXP成为J2SE核心代码后,类加载机制就换成了使用线程上下文加载器,这和JNDI的原因相似。

    好了,现在我们明白了问题的关键:这两种选择不可能适应所有情况。一些人认为线程上下文类加载器应成为新的标准。但这在不同JVM线程共享数据来沟通时,就会使类加载器的结构乱七八糟。除非所有线程都使用同一个上下文类加载器。而且,使用当前类加载器已成为缺省规则,它们广泛应用在类声明、Class.forName等情景中。即使你想尽可能只使用上下文类加载器,总是有这样那样的代码不是你所能控制的。这些代码都使用代理到当前类加载器的模式。混杂使用代理模式是很危险的。

    更为糟糕的是,某些应用服务器将当前类加载器和上下文类加器分别设置成不同的ClassLoader实例。虽然它们拥有相同的类路径,但是它们之间并不存在父子代理关系。想想这为什么可怕:记住加载并定义某个类的类加载器是虚拟机内部标识该类的组成部分,如果当前类加载器加载类X并接着执行它,如JNDI查找类型为Y的数据,上下文类加载器能够加载并定义Y,这个Y的定义和当前类加载器加载的相同名称的类就不是同一个,使用隐式类型转换就会造成异常。

     这种混乱的状况还将在Java中存在很长时间。在J2SE中还包括以下的功能使用不同的类加载器:

    * JNDI使用线程上下文类加载器

    * Class.getResource()和Class.forName()使用当前类加载器

    * JAXP使用上下文类加载器

    * java.util.ResourceBundle使用调用者的当前类加载器

    * URL协议处理器使用java.protocol.handler.pkgs系统属性并只使用系统类加载器。

    * Java序列化API缺省使用调用者当前的类加载器

    这些类加载器非常混乱,没有在J2SE文档中给以清晰明确的说明。

原文地址:

http://www.javaworld.com/javaworld/javaqa/2003-06/01-qa-0606-load.html?page=1

public abstract class ClassLoaderResolver
{
    /**
     * This method selects the best classloader instance to be used for
     * class/resource loading by whoever calls this method. The decision
     * typically involves choosing between the caller's current, thread context,
     * system, and other classloaders in the JVM and is made by the {@link IClassLoadStrategy}
     * instance established by the last call to {@link #setStrategy}.
     * 
     * @return classloader to be used by the caller ['null' indicates the
     * primordial loader]   
     */
    public static synchronized ClassLoader getClassLoader ()
    {
        final Class caller = getCallerClass (0);
        final ClassLoadContext ctx = new ClassLoadContext (caller);
        
        return s_strategy.getClassLoader (ctx); 
    }
    public static synchronized IClassLoadStrategy getStrategy ()
    {
        return s_strategy;
    }
    public static synchronized IClassLoadStrategy setStrategy (final IClassLoadStrategy strategy)
    {
        final IClassLoadStrategy old = s_strategy;
        s_strategy = strategy;
        
        return old;
    }
        
    /**
     * A helper class to get the call context. It subclasses SecurityManager
     * to make getClassContext() accessible. An instance of CallerResolver
     * only needs to be created, not installed as an actual security
     * manager.
     */
    private static final class CallerResolver extends SecurityManager
    {
        protected Class [] getClassContext ()
        {
            return super.getClassContext ();
        }
        
    } // End of nested class 
    
    
    /*
     * Indexes into the current method call context with a given
     * offset.
     */
    private static Class getCallerClass (final int callerOffset)
    {        
        return CALLER_RESOLVER.getClassContext () [CALL_CONTEXT_OFFSET +
            callerOffset];
    }
    
    private static IClassLoadStrategy s_strategy; // initialized in <clinit>
    
    private static final int CALL_CONTEXT_OFFSET = 3; // may need to change if this class is redesigned
    private static final CallerResolver CALLER_RESOLVER; // set in <clinit>
    
    static
    {
        try
        {
            // This can fail if the current SecurityManager does not allow
            // RuntimePermission ("createSecurityManager"):
            
            CALLER_RESOLVER = new CallerResolver ();
        }
        catch (SecurityException se)
        {
            throw new RuntimeException ("ClassLoaderResolver: could not create CallerResolver: " + se);
        }
        
        s_strategy = new DefaultClassLoadStrategy ();
    }
} // End of class.
You acquire a classloader reference by calling the ClassLoaderResolver.getClassLoader() static method and use the result to load classes and resources via the normal java.lang.ClassLoader API. Alternatively, you can use this ResourceLoader API as a drop-in replacement for java.lang.ClassLoader:
public abstract class ResourceLoader
{
    /**
     * @see java.lang.ClassLoader#loadClass(java.lang.String)
     */
    public static Class loadClass (final String name)
        throws ClassNotFoundException
    {
        final ClassLoader loader = ClassLoaderResolver.getClassLoader (1);
       
        return Class.forName (name, false, loader);
    }
    /**
     * @see java.lang.ClassLoader#getResource(java.lang.String)
     */   
    public static URL getResource (final String name)
    {
        final ClassLoader loader = ClassLoaderResolver.getClassLoader (1);
       
        if (loader != null)
            return loader.getResource (name);
        else
            return ClassLoader.getSystemResource (name);
    }
    ... more methods ...
} // End of class
The decision of what constitutes the best classloader to use is factored out into a pluggable component implementing the IClassLoadStrategy interface:
public interface IClassLoadStrategy
{
    ClassLoader getClassLoader (ClassLoadContext ctx);
} // End of interface
 
To help IClassLoadStrategy make its decision, it is given a ClassLoadContext object:
public class ClassLoadContext
{
    public final Class getCallerClass ()
    {
        return m_caller;
    }
    
    ClassLoadContext (final Class caller)
    {
        m_caller = caller;
    }
    
    private final Class m_caller;
} // End of class
 
ClassLoadContext.getCallerClass() returns the class whose code calls into ClassLoaderResolver or ResourceLoader. This is so that the strategy implementation can figure out the caller's classloader (the context loader is always available as Thread.currentThread().getContextClassLoader()). Note that the caller is determined statically; thus, my API does not require existing business methods to be augmented with extra Class parameters and is suitable for static methods and initializers as well. You can augment this context object with other attributes that make sense in your deployment situation.
 
I have a default strategy implementation that should work correctly in 95 percent of real-life situations:
public class DefaultClassLoadStrategy implements IClassLoadStrategy
{
    public ClassLoader getClassLoader (final ClassLoadContext ctx)
    {
        final ClassLoader callerLoader = ctx.getCallerClass ().getClassLoader ();
        final ClassLoader contextLoader = Thread.currentThread ().getContextClassLoader ();
        
        ClassLoader result;
        
        // If 'callerLoader' and 'contextLoader' are in a parent-child
        // relationship, always choose the child:
        
        if (isChild (contextLoader, callerLoader))
            result = callerLoader;
        else if (isChild (callerLoader, contextLoader))
            result = contextLoader;
        else
        {
            // This else branch could be merged into the previous one,
            // but I show it here to emphasize the ambiguous case:
            result = contextLoader;
        }
        
        final ClassLoader systemLoader = ClassLoader.getSystemClassLoader ();
        
        // Precaution for when deployed as a bootstrap or extension class:
        if (isChild (result, systemLoader))
            result = systemLoader;
        
        return result;
    }
    
    ... more methods ...
} // End of class

The logic above should be easy to follow. If the caller's current and context classloaders are in a parent-child relationship, I always choose the child. The set of resources visible to a child loader is normally a superset of classes visible to its parent, so this feels like the right decision as long as everybody plays by J2SE delegation rules.

It is when the current and the context classloaders are siblings that the right decision is impossible. Ideally, no Java runtime should ever create this ambiguity. When it happens, my code chooses the context loader: a decision based on personal experience of when things work correctly most of the time. Feel free to change that code branch to suit your taste. It is possible that the context loader is a better choice for framework components, and the current loader is better for business logic.

Finally, a simple check ensures that the selected classloader is not a parent of the system classloader. This is a good thing to do if you are developing code that might be deployed as an extension library.

Note that I intentionally do not look at the name of resources or classes that will be loaded. If nothing else, the experience with Java XML APIs becoming part of the J2SE core should have taught you that filtering by class names is a bad idea. Nor do I trial load classes to see which classloader succeeds first. Examining classloader parent-child relationships is a fundamentally better and more predictable approach.

Although Java resource loading remains an esoteric topic, J2SE relies on various load strategies more and more with every major platform upgrade. Java will be in serious trouble if this area is not given some significantly better design considerations. Whether you agree or not, I would appreciate your feedback and any interesting pointers from your personal design experience.

Android解析ClassLoader(二)Android中的ClassLoader 相关文章 Java虚拟机系列 Android系统启动系列 Android解析ClassLoader系列 前言 在上一篇文章我们学习了Java的ClassLoader,很多同学会把Java和Android的ClassLoader搞混,甚至会认为Android中的ClassLoader和Java中的ClassLoader是一样的,这显然是不对的。这一篇文章我们就来学习Android中的C... 阅读详情

相关推荐

JDK源码之ClassLoader

Java 中,自定义类加载器允许从标准的 classpath 之外的位置,如特定的目录、网络或者加密文件动态加载类。自定义类加载器的核心在于理解 Java 的双亲委派模型。该模型规定:当一个类加载器收到加载请求时,它首先不会自己尝试去加载,而是将这个请求委托给父类加载器去完成。只有当所有父类加载器都无法完成加载时,当前加载器才会调用自己的 findClass 方法。因此,实现自定义类加载器的标准做法是继承类,并重写 findClass 方法。

qq_25139621的博客 1122

走出类加载器迷宫

这是前几天在看类加载器机制时搜到的一篇旧文,网上搜了搜相应的中文资料,感觉很多意思没有翻译出来,这两天我试着自己翻译了一下,供同道参考。英文文章地址:Find a way out of the ClassLoader maze     走出类加载器迷宫(本人翻译,转载请注明出处)   系统类加载器, 当前类加载器, 上下文类加载器? 你应该用哪一个? By Vladimir Rou...

预流 245

基于MOS的防反接电路

防反接电路一般用于电源入口处,在电源反接时,保证电源入口之后的电路不会受到损害。一般在电源电流较小、对1V左右的压降不敏感时可以使用二极管进行防反接。防反接的形式有直接在正极串联二极管(如图1),或使用整流桥的方式(如图2),但这两种方式均存在:1、存在二极管的管压降;2、过大电流时,二极管消耗的功率大,发热严重。这两个问题。 图 1 二极管防反接电路 图 2 整流管防反接电路 1、基于NMO...

qq_42682826的博客 2万+

关于Junit报java.lang.ClassNotFoundException: javax.servlet.ServletResponse的解决方法

在使用Junit对Spring MVC的service层进行测试时,当使用以下两个注解,且配置正确。 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "file:src/main/webapp/WEB-INF/applicationContext.xml") 运行Junit用例时,报如下错误:...

u011472058的博客 2098

MT25QL256完整操作指南:从ID读取到4KB扇区擦除(附华大SPI例程)

本文提供了MT25QL256 NOR FLASH在华大MCU上的完整驱动开发指南。内容涵盖从SPI初始化、器件ID读取、状态寄存器查询,到核心的页编程和4KB扇区擦除操作,并分享了构建健壮驱动框架、处理跨页写入及常见调试问题的实战经验。

tt34567的博客 886

生物信息学名词解释 | K-mer (长度为k的短序列)

K-mer 何谓K-mer 所谓Kmer,即为一段长度为k的DNA片段,是由测序reads剪切一部分得到的。k为一个奇数,k=几,就为几mer。 比如:我的测序reads长度为100bp,我将这100bp打断成17bp的短片段,打断后的17bp段片段就叫17mer,可以获得(100-17+1)条k-mer序列。 Table1 k-mers for GTAGAGCTGT. k k-mers 1 G,T,A,G,A,G,C,T,G,T 3 GTA,TAG,AGA,GAG,AGC,GCT,CT

qq_50637636的博客 2万+

类加载七:调用ClassLoader类的loadClass方法加载一个类,并不是对类的主动使用

七、调用ClassLoader类的loadClass方法加载一个类,并不是对类的主动使用。代码如下: public class MyTest12 { public static void main(String[] args) throws ClassNotFoundException { ClassLoader loader = ClassLoader.getSystemClassLoader(); Class<?> clazz = loader.loadClass("com.

qq_27114677的博客 2059

FISCO-BCOS配置及使用控制台

如果因为网络问题长时间无法下载,请尝试:curl -#LO https://gitee.com/FISCO-BCOS/console/raw/master-2.0/tools/download_console.sh && bash download_console.sh。为了可以更加方便快捷,所以HelloWorld合约已经被内置了,位于控制台下面的contracts/solidity/HelloWorld.sol。用于获取和设置name的值,合约代码如下。部署成功会返回合约地址。

qq_51562644的博客 831

关于Classloader的总结!loadClass的分析和加载细节的分析

Java代码   package com.test.one;  import java.io.BufferedInputStream;  import java.io.ByteArrayOutputStream;  import java.io.File;  import java.io.FileInputStream;  import java.lang.reflect.Method;

脚印 8653

IP路由原理、静态路由及动态路由区分

什么是路由 路由是指导IP报文发送的路径信息。 直连路由:目的地所在网络与路由器直接相连。 间接路由:目的地所在网络与路由器非直接相连。 静态路由是由管理员手工配置而生成的路由。 动态路由路由器使用路由协议从其他路由器那里获悉的路由。

weixin_42227328的博客 5470

RK3588平台MIPI DSI屏幕调试记录

本文是基于RK3588平台,MIPI屏调试总结。调试过程中遇到的问题:一开始很久没亮,测量了电源电压和波形都正常。后来换了一组板子和屏幕做对比。最后是因为屏幕的座子没焊接好,二是接MIPI屏幕的排线太长会影响数据传输。换了一根短的线之后屏幕亮起。vp2vp3。

bingdund的博客 1390

ClassLoader加载指定的类需注意六个细节或报ClassNotFundEception异常总结

项目中,加载指定的类反射调用方法一直报类找不到,经过数百次的测试,对这种问题有了一个重新的认识,特总结、记录、分享如下: 1、路径中尽可能用"/"或者File.separator()而非“\\”,路径中“\”是windows平台的,linux平台中是“/”,java中也是用“/”作为路径的,所以为了跨平台和更规范,建议采用前者 2、在当前的环境中获取绝对路径后,尽可能的replace(“\\”...

iteye_6274的博客 223

元宇宙通证- 九、IT/ICT发展史全景长图

九、IT/ICT发展史全景长图 (一)综述 IT是Information Technology的缩写,意为”信息技术“,包含现代计算机、网络、通信等信息领域技术。自1844年人类历史上发出来第一份电报,实现了信息的长图传输,信息技术发展的历史大幕随之拉开。信息技术的普遍应用,是进入信息社会的标志。 IT技术最早发源于半导体,然后发展处计算机、通信网和互联网,进而演进到现在的AI、XR、计算机、大数据、物联网、工业互联网、区块链、量子计算等。 有人说元宇宙是下一代互联网,有人说元宇宙是互联网的终极形态。 我们

沧海之巅的专栏 1万+

java类classloader_ClassLoader专题(一):ClassLoader基础

JVM启动,会形成3个类加载器组成的初始化加载器层次结构:bootstap classloader (加载核心类)||extension classloader(加载ext(目录),即java.ext.dirs())||system classloader (加载-classpath或者java.class.path或者CLASSPATH)ClassLoader机制:a)全盘负责:一个clas...

weixin_30415591的博客 211

CloudCompare源码分析之CCCoreLib模块:ICP算法(阅读经典)

CloudCompare源码分析之CCCoreLib模块:ICP算法(阅读经典)

dayuhaitang1的博客 2551

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

ClassLoader专题(一):ClassLoader基础

ClassLoader 专题(一): ClassLoader ...

cuiyi's blog(崔毅 crazycy) 186

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

ClassLoader专题(三):引文

     摘要: ClassLoader 专题(一): ClassLoader 基础 ClassLoader 专题(二):从 Servlet 容器看 ClassLoader 机制的妙用     ClassLoader专题(三):引文   Find a way out of the ClassLoader maze System, current, context?...  阅读全文crazyc...

cuiyi's blog(崔毅 crazycy) 125

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

VB摄像头监控

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

【JVM技术专题】深度分析ClassLoader运行机制「 入门篇」

Java中的所有类必须被装载到jvm中才能运行,这个装载工作是由jvm中的类装载器完成的,类装载器所做的工作实质是把类文件从硬盘(或者其他介质)读取到JVM内存中,JVM加载类的时候,都是通过ClassLoader的loadClass()方法来加载class的,loadClass使用双亲委派模式。 文章目录前提概要源码分析ClassLoader的介绍大致意思如下loadClass方法的实现方式:还是来看sun公司对该方法的解释:大致内容如下总结 源码分析 ClassLoader的介绍 C

世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。 313

Android解析ClassLoader(一)Java中的ClassLoader

系统提供的类加载器只能够加载指定目录下的jar包和Class文件,如果想要加载网络上的或者是D盘某一文件中的jar包和Class文件则需要自定义ClassLoader。定义一个自定义ClassLoade并继承抽象类ClassLoader。复写findClass方法,并在findClass方法中调用defineClass方法。下面我们就自定义一个ClassLoader用来加载位于D:\lib的Class文件。

qq_39431405的博客 708

详解JAVA ClassLoader

ClassLoader,翻译成中文就是“类加载器”。对于普通的 Java 开发者来说,可能接触得比较少,但对于框架开发者或者需要深度定制 Java 应用的开发者来说,这是一个非常重要的工具。理解 ClassLoader 的工作机制,能帮助我们更好地理解 Java 程序的运行原理,还能指导我们编写出更高效的代码。ClassLoader 的主要功能是将.class文件加载到 JVM(Java 虚拟机)中,使程序能够正常运行。不过,JVM 在启动时并不会一次性将所有的类文件加载到内存中,而是根据需要动态加载。

weixin_60669486的博客 1450

JVM实战:ClassLoader的原理及应用

ClassLoader(类加载器) 是 JVM(Java 虚拟机)的核心组件之一。它的核心职责只有一件事:将内存外部的 .class 字节码文件,加载到 JVM 的方法区中,并转为 java.lang.Class 对象// null= null) {Java提供了抽象类java.lang.ClassLoader,所有用户自定义的类加载器都应该继承ClassLoader类,所以我们先看一下ClassLoader的逻辑,看一下它加载类的主要逻辑我截取了3个重要的方法。

小识的博客 691
上一篇: WEB应用中读取配置文件
下一篇: 进行 HTML、JavaScript 和 Ajax 开发和调试的必用工具
waveeee
博客等级 码龄21年 2粉丝 11原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值