An introduction to java class loader

An introduction to java class loader

AClassLoader’s basic purpose is to service a request for a class. TheJVM needs a class, so it asks the ClassLoader, by name, for thisclass, and the ClassLoader attempts to return a Class object thatrepresents the class. By overriding different methods correspondingto different stages of this process, you can create a customClassLoader.

Thefollowing is a high-level class-loading algorithm executed by aClassLoader when a client requests it to load a class:

Acheck is performed to see if the requested class has already beenloaded by the current ClassLoader. If so, the loaded class isreturned and the request is completed. The JVM caches all theclasses that are loaded by a ClassLoader. A class that haspreviously been loaded by a ClassLoader is not loadedagain.(ClassLoader#findLoadedClass())

Ifthe class is not already loaded, the request is delegated to theparent ClassLoader, before the current ClassLoader tries to load it.This delegation can go all the way up to the bootstrap ClassLoader,after which no further delegation is possible.(ClassLoader#findSystemClass())

Ifa parent fails to return a class because it was unable to load it,the current ClassLoader will then try to search for the requestedclass. Each ClassLoader has defined locations where it searches forclasses to load. For instance, the bootstrap ClassLoader searches inthe locations (directories and zip/jar files) specified in thesun.boot.class.pathsystem property. The system ClassLoader searches for classes in thelocations specified by the classpath (set as the java.class.pathsystem property) command-line variable passed in when a JVM startsexecuting. If the class is found, it’s loaded into the system andreturned, completing the request.

Ifthe class is not found, a java.lang.ClassNotFoundException isthrown.

Injava 1.1, there were no relations between system class loader(responsible for loadingin the Java runtime, the application, and classes and resources inthe application’s CLASSPATH) and applet class loader (loaded classesover networks).

SinceJava 1.2 we have three types of class loaders:

Classloaders created automatically by the JVM

Programdefined class loaders

Contextclass loaders.

There are three Class loaders infirst group:

bootstrapclass loader – loads classes from ../jre/lib/rt.jar It is the "root"in the class loader hierarchy.

extensionsclass loader – loads classes from ../jre/lib/ext/*.jar

systemclass loader – it is responsible for loading in the application, aswell as for loading classes and resources in the application’sCLASSPATH.

Second group includes:

systemclass loader – parent class loader by default

additionalparent class loader can be specified explicitly

Third group is context classloader. A thread’s context class loader is, by default, set to thecontext class loader of the thread’s parent. The hierarchy of threadsis rooted at the primordial thread (the one that runs the program).The context class loader of the primordial thread is set to the classloader that loaded the application. So unless you explicitly changethe thread’s context class loader, its context class loader will bethe application’s class loader. That is, the context class loader canload the classes that the application can load. Tochange a thread’s context class loader, useThread.setContextClassLoader().

For more information:

Thebasics of Java class loaders

Securityand the class loader architecture

TheClass Loader Architecture

Understandingthe Java ClassLoader

UnderstandingNetwork Class Loaders

Internalsof Java Class Loading

java.lang.ClassLoader

空虚无聊的时候就读书,但一定得有自己的生活目标和计划。

An introduction to java class loader

相关文章:

你感兴趣的文章:

标签云: