首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

ClassLoader 加载种的顺序

2012-10-31 
ClassLoader 加载类的顺序参照API Doc:Loads the class with the specifiedbinary name. The default impl

ClassLoader 加载类的顺序

参照API Doc:

Loads the class with the specifiedbinary name. The default implementation of this method searches for classes in the following order:

    InvokefindLoadedClass(String)to check if the class has already been loaded.

    Invoke theloadClassmethod on the parent class loader. If the parent isnullthe class loader built-in to the virtual machine is used, instead.

    Invoke thefindClass(String)method to find the class.

当客户程序请求类装入器装入某个类时,类装入器按照下面的算法装入一个类: 

首先执行一次检查,查看客户程序请求的类是否已经由当前的类装入器装入。如果是,则返回已装入的类,请求处理完毕。JVM缓冲了类装入器装入的所有类,已经装入的类不会被再次装入。

 如果尚未装入类,则装入类的请求被委托给父类装入器,这个操作发生在当前的类装入器尝试装入指定的类之前。委托装入类的操作一直向上传递,直至bootstrap类装入器,如前所述,bootstrap类装入器是处于树形结构的顶层,它不再有可委托的父类装入器。

 如果父类装入器未能装入指定的类,则当前的类装入器将尝试搜索该类。每一个类装入器有预定义的搜索和装入类的位置。例如,bootstrap类装入器搜索 sun.boot.class.path系统属性中指定的位置(包括目录和zip、jar文件),system类装入器搜索的位置由JVM开始运行时传入的CLASSPATH命令行变量指定(相当于设置java.class.path系统属性)。如果找到了类,则类装入器将类返回给系统,请求处理完毕。

 ⑷如果找不到类,则抛出java.lang.ClassNotFoundException异常。

热点排行