class loader talking about

Source: Internet
Author: User

1. Basic Concepts

As the name suggests, the ClassLoader (class loader) is used to load Java classes into Java virtual machines. In general, Java virtual machines use Java classes in the following ways: Java source programs (. java files) are converted to Java byte code (. class files) after being compiled by the Java compiler. The class loader is responsible for reading Java byte code and converting it into an instance of the Java.lang.Class class. Each of these strengths is used to represent a Java class. The Newinstance () method of this instance allows you to create an object of that class. The actual situation can be more complex, such as Java byte Code gram 鞥 is dynamically generated through the tool, or may be downloaded through the network. Basically, all ClassLoader are an instance of the Java.lang.ClassLoader class.

2, Java.lang.ClassLoader class introduction

The basic responsibility of the Java.lang.ClassLoader class is to find or generate a byte code for the generator based on the name of a specified class, and then define a Java class from those byte codes, an instance of the Java.lang.Class class. For the first time, ClassLoader is also responsible for loading resources needed for Java applications, such as image files and configuration files. To complete this responsibility for the load class, ClassLoader provides a series of methods, and the more important approach is shown in table one.

Table 1. Methods associated with loading classes in ClassLoader
Method Description
GetParent () Returns the parent class loader for the class loader
LoadClass (String name) Load class with name named, the result returned is an instance of the Java.lang.Class class
Findclass (String name) Finds a class with the name named, and the result returned is an instance of the Java.lang.Class class
Findloadedclass (String name) Finds a class that has already been loaded with the name named, and the result returned is an instance of the Java.lang.Class class
DefineClass (String name, byte[] B, int off, int len) Converts the content in byte array B to a Java class, and the result returned is an instance of the Java.lang.Class class. This method is declared as the final type.
Resolveclass (class<?> c) Link to the specified Java class


For the method given in table 1, the value of the name parameter representing the class name is the binary name (binary) of the class. Note that the representation of the inner class, such as the Com.example.Good representation, is required.

3, the class loader tree-like structure

Class Loaders in Java can be roughly divided into two categories, one provided by the system and the other by Java application developers. The system provides class loading with the following three main:

Boot class loader (bootstrap class loader): It is used to load Java core libraries, which are implemented in native code and do not inherit from Java.lang.ClassLoader.

Extended class loader (extension class loader): It is used to load Java extension libraries. The implementation of the Java Virtual Machine provides an extension library directory. The class loader finds and loads Java classes in this directory.

System class Loader (loader): It loads Java classes based on the Java application's Classpath (CLASSPATH). In general, Java-applied classes are loaded by it. It can be obtained by Classloader.getsystemclassloader (). In addition to the system-supplied class loader surprises, developers implement their own classloader by inheriting the Java.lang.ClassLoader class to meet some special requirements.

All class loaders have one loader in addition to the boot class loader. The GetParent () method given in table 1 can be obtained. For a system-supplied ClassLoader, the parent class loader of the System class loader is the extended class loader, and the parent class loader of the extended class loader is the boot class loader; For a class loader written by a developer, its parent class is a system class loader.

Fig. 1 schematic diagram of the tree-like structure of class loader

5, the class loader agent mode

When the class loader tries to find its own byte code for a class and defines it, it first proxies its parent class loader to the parent ClassLoader to try to load the class, and so on. Before introducing the motives behind the agent model, it is first necessary to explain how the Java virtual machine determines that two Java classes are the same. The Java virtual machine depends not only on whether the full name of the class is the same, but also on whether the class loading the class loads the same. The two classes are considered to be identical only if they are the same. Even with the same byte code, the classes that are obtained after loading the class loader are different. such as a Java class come.example.Sample, compiled after the production of bytecode file Sample.class. Two different class loaders Classloadera and Classloaderb read the Sample.class file separately and define two instances of the Java.lang.Class class to represent this class. These two examples are different. For Java virtual machines, they are different classes. An attempt to assign values to objects of these two classes throws a Run-time exception classcastexception.

6, the process of loading classes

before introducing the proxy mode of the ClassLoader, it was mentioned that the class loader would first delegate to other classes to load a class. This means that the class loader that actually completes the loading of the class and the ClassLoader that started the load process may not be the same. The true completion of class loading is accomplished by invoking DefineClass, while the loading process of the startup class is implemented by invoking LoadClass. The former is called a class-defined loader (defining loader), which is called the initial loader (initiating loader). The definition loader for a class is used when the Java Virtual machine judges whether two classes are the same. That is, which class loader starts the loading process of the class is not important, it is important to ultimately define the loader for this class. The Association of two kinds of loaders is that the definition loader of a class is the initial loader of the other classes it references. If the class Com.example.Outer references a class com.example.Inner, the definition loader for the class Com.example.Outer is responsible for starting the loading process of the class Com.example.Inner.

7, the thread's context class loader

The thread Context class loader (contextual class loader) has been introduced from JDK 1.2. The methods in class Java.lang.Thread Getcontextclassloader () and Setcontextclassloader (ClassLoader cl) are used to get and set the thread's context class loader. If the Setcontextclassloader (ClassLoader cl) method is not set, the thread inherits the context class loader of its parent thread. The context class loader for the initial thread that the Java application runs is the System class loader. Code that runs in threads can load classes and resources through such a loader.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.