`
yidongkaifa
  • 浏览: 4153343 次
文章分类
社区版块
存档分类
最新评论

Understand ClassLoader in Java

 
阅读更多
A class loader is an object that is responsible for loading classes. The classClassLoaderis an abstract class. Given thebinary nameof a class, a class loader should attempt to locate or generate data that constitutes a definition for the class. A typical strategy is to transform the name into a file name and then read a "class file" of that name from a file system.

EveryClassobject contains areferenceto theClassLoaderthat defined it.

Classobjects for array classes are not created by class loaders, but are created automatically as required by the Java runtime. The class loader for an array class, as returned byClass.getClassLoader()is the same as the class loader for its element type; if the element type is a primitive type, then the array class has no class loader.

Applications implement subclasses ofClassLoaderin order to extend the manner in which the Java virtual machine dynamically loads classes.

Class loaders may typically be used by security managers to indicate security domains.

TheClassLoaderclass uses a delegation model to search for classes and resources. Each instance ofClassLoaderhas an associated parent class loader. When requested to find a class or resource, aClassLoaderinstance will delegate the search for the class or resource to its parent class loader before attempting to find the class or resource itself. The virtual machine's built-in class loader, called the "bootstrap class loader", does not itself have a parent but may serve as the parent of aClassLoaderinstance.

Class loaders that support concurrent loading of classes are known asparallel capableclass loaders and are required to register themselves at their class initialization time by invoking theClassLoader.registerAsParallelCapablemethod. Note that theClassLoaderclass is registered as parallel capable by default. However, its subclasses still need to register themselves if they are parallel capable.
In environments in which the delegation model is not strictly hierarchical, class loaders need to be parallel capable, otherwise class loading can lead to deadlocks because the loader lock is held for the duration of the class loading process (seeloadClassmethods).

Normally, the Java virtual machine loads classes from the local file system in a platform-dependent manner. For example, on UNIX systems, the virtual machine loads classes from the directory defined by theCLASSPATHenvironment variable.

However, some classes may not originate from a file; they may originate from other sources, such as the network, or they could be constructed by an application. The methoddefineClassconverts an array of bytes into an instance of classClass. Instances of this newly defined class can be created usingClass.newInstance.

The methods and constructors of objects created by a class loader may reference other classes. To determine the class(es) referred to, the Java virtual machine invokes theloadClassmethod of the class loader that originally created the class

For example, an application could create a network class loader to download class files from a server. Sample code might look like:

   ClassLoader loader= new NetworkClassLoader(host,port);
   Object main= loader.loadClass("Main", true).newInstance();
       ...
 

The network class loader subclass must define the methodsfindClassandloadClassDatato load a class from the network. Once it has downloaded the bytes that make up the class, it should use the methoddefineClassto create a class instance. A sample implementation is:

     class NetworkClassLoader extends ClassLoader {
         String host;
         int port;

         public Class findClass(String name) {
             byte[] b = loadClassData(name);
             return defineClass(name, b, 0, b.length);
         }

         private byte[] loadClassData(String name) {
             // load the class data from the connection
             ...
         }
     }
 

分享到:
评论

相关推荐

    破解java加密的ClassLoader.java,在classloader植入破解代码

    破解java加密的ClassLoader.java,在classloader植入破解代码

    Java SE: ClassLoader in depth

    对于标题“Java SE: ClassLoader in depth”和描述中提到的“源码”、“工具”,实际上在给出的内容部分并没有任何与Java ClassLoader深入相关的技术信息。所提供的内容实际上是一份公司入职报到的指南,与Java编程...

    java应用程序类加载器,ClassLoader for java Application

    **java应用程序类加载器(ClassLoader for java Application)**: 当我们创建一个Java应用程序时,比如通过`java MainClass`命令启动,实际上是由应用程序类加载器来执行的。它查找并加载包含主类(Main-Class ...

    java classloader

    Java ClassLoader是一个核心的Java运行时组件,负责加载类到Java虚拟机(JVM)中。它是Java平台的独特特性,因为它允许动态加载类,增强了软件的可扩展性和灵活性。这篇博文(虽然链接不可用)可能深入探讨了...

    Java ClassLoader定制实例

    在Java编程语言中,ClassLoader是一个至关重要的组成部分,它负责加载类到JVM(Java虚拟机)中。理解ClassLoader的工作原理以及如何定制它,对于深入学习Java的运行机制和进行高级应用开发具有重要意义。本篇文章将...

    Java ClassLoader学习总结

    Java ClassLoader学习总结 Java 类加载机制是 Java 中一个非常重要的机制,它负责加载 Class 文件到 JVM,以供程序使用。ClassLoader 是 Java 中的一个抽象类,它的主要作用是加载 Class 文件到 JVM 中。...

    java classloader classpath 张孝祥

    ### Java ClassLoader与ClassPath详解 #### 一、概述 在Java编程中,类加载机制是十分关键的一个环节。类加载器(`ClassLoader`)负责将编译后的`.class`文件加载到Java虚拟机(JVM)中执行,而类路径(`ClassPath...

    java ClassLoader机制及其在OSGi中的应用

    Java ClassLoader机制是Java虚拟机(JVM)中一个至关重要的组成部分,它的主要任务是将类的.class文件加载到JVM中,使得程序能够运行。ClassLoader不仅负责类的加载,还涉及类的验证、初始化等一系列过程。理解...

    理解Java ClassLoader机制

    Java ClassLoader机制是Java运行时环境中的核心组件之一,它负责加载类到JVM(Java虚拟机)中,使得程序能够执行。理解ClassLoader的工作原理对于优化应用性能、处理类加载问题以及实现自定义加载器至关重要。 首先...

    Java ClassLoader原理

    ### Java ClassLoader原理详解 #### 摘要 本文探讨了Java虚拟机(JVM)中的一个重要特性:动态类加载(Dynamic Class Loading)。这一机制为Java平台提供了强大的能力,允许在运行时安装软件组件,例如从网络下载...

    Understanding the Java ClassLoader

    ### Java ClassLoader理解详解 #### 一、引言 在商业流行的编程语言中,Java以其独特的运行机制脱颖而出:它在Java虚拟机(JVM)上运行。这意味着编译后的程序采用一种特殊的、与平台无关的格式,而不是针对特定...

    java中classLoader的使用

    Java中的类加载器(ClassLoader)是Java虚拟机(JVM)的一个重要组成部分,它负责将类的.class文件从文件系统或者网络中加载到内存中,并转换为对应的Class对象。类加载器的工作流程主要包括加载、验证、准备、解析...

    ClassLoader

    ### Java虚拟机中ClassLoader概述与双亲委托机制详解 #### 一、ClassLoader概念与作用 在Java编程语言中,`ClassLoader`是一个非常重要的组件,它负责加载程序运行所需的类文件到Java虚拟机(JVM)中。`ClassLoader`...

    classloader

    Java ClassLoader是Java运行时系统的关键但经常被忽视的组件,负责在运行时查找和加载类文件。通过创建自定义ClassLoader,你可以定制JVM,使类文件的引入方式完全重新定义,这提供了很多实用和有趣的可能。这篇教程...

    Java ClassLoader Tutorial.zip

    Java 类加载器(ClassLoader)是Java虚拟机(JVM)的重要组成部分,它负责在运行时查找并加载类到JVM中。这个教程将深入探讨ClassLoader的工作原理、类型以及如何自定义类加载器。 一、Java ClassLoader 基础 1. 类...

    ClassLoader 详解.doc

    Bootstrap ClassLoader并非继承自java.lang.ClassLoader,而是由JVM直接实现,因此在Java代码中无法直接获取和操作。它加载的类库路径可以通过-Xbootclasspath或-Dsun.boot.class.path系统属性进行设置。 2. ...

    classloader-playground, 一个简单的java依赖隔离容器类.zip

    《深入理解Java类加载器:基于classloader-playground开源项目》 在Java世界里,类加载器(ClassLoader)是理解JVM工作原理的关键一环。它负责将字节码文件(.class)从磁盘、网络或其他数据源加载到内存,并转化为...

Global site tag (gtag.js) - Google Analytics