Java Class Loader

转载 2007年09月13日 21:17:00

Every time you create an instance of a Java class, the class must first be loaded into memory. The JVM uses a class loader to load classes. The class loader normally searches some core Java libraries and all directories included in the CLASSPATH environment variable. If it does not find the required class, it throws a ClassNotFoundException.

Starting from J2SE 1.2, the JVM employs three class loaders: bootstrap class loader,extension class loader, and system class loader. Each of the three class loaders has a parent-child relationship with each other, in which the bootstrap class loader sits at the topmost of the hierarchy and the system class loader at the bottom.

The bootstrap class loader is used to bootstrap the JVM. It starts working whenever you call the java.exe program. As such, it must be implemented using the native code because it is used to load the classes required for the JVM to function. Also, it is responsible for loading all the core Java classes, such as those in java.lang and packages. The bootstrap class loader searches the core libraries such as rt.jar, i18n.jar, etc. Which libraries are searched depends on the version of the JVM and the operating system.

The extension class loader is responsible for loading classes in a standard extension directory. This is to make the programmer’s life easier because they can just copy jar files into this extension directory and the jar files will be searched automatically. The extension library differs from one vendor to another. Sun’s JVM’s standard extension directory is /jdk/jre/lib/ext.

The system class loader is the default class loader and searches the directories and jar files specified in the CLASSPATH environment variable.

So, which class loader does the JVM use? The answer lies in the delegation model,which is there for security reasons. Every time a class needs to be loaded, the system class loader is first called. However, it does not load the class right away. Instead, it delegates the task to its parent, the extension class loader. The extension class loader also delegates it to its parent, the bootstrap class loader. Therefore, the bootstrap class loader is always given the first chance to load a class. If the bootstrap class loader can’t find the class needed, the extension class loader will try to load the class. If the extension class loader also fails, the system class loader will perform the task. If the system class loader can’t find the class, a ClassNotFoundException is thrown. Why the round trip?

The delegation model is very important for security. As you know, you can use the security manager to restrict access to a certain directory. Now, someone with malicious intents can write a class called java.lang.Object that can be used to access any directory in the hard disk. Because the JVM trusts the java.lang.Object class, it
won’t watch its activity in this regard. As a result, if the custom java.lang.Object was allowed to be loaded, the security manager would be easily paralyzed. Fortunately, this won’t happen because of the delegation model. Here is how it works.

When the custom java.lang.Object class is called somewhere in the program, the system class loader delegates the request to the extension class loader, which delegates to the bootstrap class loader. The bootstrap class loader searches its core libraries, and finds the standard java.lang.Object and instantiates it. As a result, the custom java.lang.Object will never be loaded.

The great thing about the class loading mechanism in Java is that you can write your own class loader by extending the abstract java.lang.ClassLoader class. 

Java类加载器(Class Loader)之详解

本文主要总结一下我对Java类加载器(Class Loader)认识,如有不准确之处还望大侠不吝赐教! 关键字:Java,类加载器(Class Loader) 主要从如下几个部分进行介绍: ...
  • Radic_Feng
  • Radic_Feng
  • 2011年10月23日 18:31
  • 8206

java class loader

ProblemIn a project, We use a stub jar file generated by Axis2. It always throw: java.lang.NoSuchFie...
  • FireCoder
  • FireCoder
  • 2010年03月22日 09:28
  • 1658

java classLoader体系结构使用详解

java classLoader体系结构使用详解,代码下载地址:
  • yaerfeng
  • yaerfeng
  • 2014年05月04日 09:34
  • 11108

Bootstrap:class loader creation threw exception异常解决方法

1,使用低于Tomcat 5.0.27版本,如Tomcat 5.0.252,在lomboz插件com.objectlearn.jdt.j2ee_3.0.1文件夹下有个servers 目录,该目录里有文...
  • blue_fire2008
  • blue_fire2008
  • 2008年04月29日 20:24
  • 3948

Android studio异常Class not found using the boot class loader; no stack available

01-16 01:50:58.298 9057-9057/? E/AndroidRuntime: FATAL EXCEPTION: main ...
  • sinat_35670989
  • sinat_35670989
  • 2017年01月16日 15:09
  • 3715


  • hacke2
  • hacke2
  • 2014年04月14日 23:16
  • 7837

interface XXX is not visible from class loader

java.lang.IllegalArgumentException: interface XXX is not visible from class loader //super.getClass...
  • u013244038
  • u013244038
  • 2017年03月18日 11:23
  • 1754


interface XXX is not visible from class loader---(java反射机制)2008-04-20 19:29今天做了一个小程序,有关java反射机制的程序,...
  • xgbjmxn
  • xgbjmxn
  • 2011年03月09日 14:10
  • 1041

自定义class loader

原作者文章出处:自定义class loader 上图为JDK 8中ClassLoader的族谱,可见除了总所周知的AppClassLoader和ExtClassLoader外,JDK中还有很多...
  • u012542422
  • u012542422
  • 2016年02月23日 09:50
  • 307

Class not found using the boot class loader; no stack available

Class not found using the boot class loader; no stack available
  • android_yck
  • android_yck
  • 2016年08月01日 17:02
  • 3027
您举报文章:Java Class Loader