Java class loading principle and Class Loader

Source: Internet
Author: User

Unlike other languages, Java runs on a Java Virtual Machine (JVM ). This means that the compiled code is saved in a platform-independent format, rather than running on a specific machine. This format is significantly different from the traditional executable code format. Specifically, unlike C or C ++ programs, Java programs are not an independent executable file, but composed of many separate class files. Each class file corresponds to a Java class. In addition, these class files are not immediately loaded into the memory, but loaded only when the program needs them. A Class Loader is a tool used by the Java Virtual Machine to load classes into the memory. In addition, Java class loaders are also implemented in Java. In this way, you do not need to have a deep understanding of the Java Virtual Machine to easily create your own class loader.

 

 

 

 

Java Class Loader:

Java has three types of loaders by default: The bootstrap loader, the extension class loader, and the system class loader (also called the application Class Loader)

Classloaders are one of the most powerful features of Java. However, developers often forget to load components by class. The class loader is the class responsible for searching and loading class files at runtime. Java allows different class loaders and even custom class loaders.

 

Java programs contain many class files, each of which corresponds to a single Java class. These class files are not static C programs, and are loaded into memory at one time. They need to be loaded at any time. This is a distinctive place for the class loader. It obtains the platform-independent bytecode from the source file (usually a. Class or. jar file) and loads them into the JVM memory, so they can be interpreted and executed. By default, each class of the application is loaded by Java. Lang. classloader. Because it can be inherited, You can freely enhance its functions.

 

Reason for using the custom Class Loader

The default java. Lang. classloader can only load classes from the local file system. Java is designed to have enough elastic loading classes regardless of the local disk or network, and can handle special things before loading. For example, an application can check the updated version of the inserted class on the web site or FTP and automatically verify the digital signature to ensure that the trusted code is executed. Many well-known software use their own class loaders.

 

Generally, the default loader is the so-called Bootstrap Class Loader. It is responsible for loading key classes such as Java. Lang. Object and loading the runtime code of other Rt. Jar files to the memory. Because the Java language specification does not provide detailed information about the bootstrap class loader, different JVMs may have different class loaders. If applets is running on the webpage, it uses a custom class loader. The applet reader embedded in the browser contains a class loader that can access the site on the remote server. It can load the original bytecode files through HTTP and convert them into classes in JVM.

 

The Class Loader (except the bootstrap Class Loader) has a parent class loader, which is an instance of the basic loader. The most important thing is to set the correct parent loader. Then, you can use the getparent () method of the class loader to implement delegate class requests (for example, when the custom Class Loader cannot find a class using a special method ). In this case, the parent loader must be used as a parameter of the Java. Lang. classloader constructor:

Public class myclassloader extends classloader {public myclassloader () {super (myclassloader. Class. getclassloader ());}}

 

 

 

The loadclass (string name) method is the entry of classloader. The name parameter is a fully qualified class name (fqcn), for example, a package name. If the parent loader is set correctly, when the loadclass (string name) method in the myclassloader is requested to load the class, but the class to be loaded is not found, the parent loader is first asked. If the parent loader cannot find this class, call the findclass (string name) method. By default, the findclass (string name) throws the classnotfoundexception exception, which many developers know. Developers of custom class loaders want to skip this method when inheriting from Java. Lang. classloader.

 

The findclass () method aims to accommodate all the specialized code for myclassloader, and does not need to repeat other Code (for example, calling the system classloader when loading fails ). In this method, classloader needs to obtain the bytecode from the original file. Once the bytecode is found, the defineclass () method is called. It is very important for the classloader instance to call this method. Therefore, if two classloader instances define bytecode from different or identical original files, the classes to be defined will also be treated differently.

 

We provide two similar class loaders, myclassloader1 and myclassloader2. Both of them can find the mycoolclass bytecode from the same source file. If a program uses these two loaders to independently load the mycoolclass instance (coolclass1 is loaded through myclassloader1 and coolclass2 is loaded through myclassloader2), mycoolclass. class can be defined independently. Run the following code:

Mycoolclass coolclass1 = (mycoolclass) coolclass2;

 

A classcastexception exception is returned. (Developers often encounter this situation if they do not have a good understanding of the class loading mechanism .) Because they are defined by different loaders, JVM regards them as different classes. Although they are classes of the same type and loaded from the same source file, coolclass1 and coolclass2 variables are not compatible.

 

Whether or not the findclass () or loadclass () is skipped, The getsystemclassloader () method directly accesses the system classloader in the form of an actual classloader object. You can also call the findsystemclass (string name) method for indirect access. The getparent () method allows you to obtain the parent loader. Listing a provides examples of user-defined class loaders that can be run.

 

Why create a class loader?
Since Java virtual gold already has a class loader, do we need to create another one ourselves? Good question. The default Class Loader only knows how to load classes from the local system. When your program is fully compiled on the local machine, the default Class Loader generally works well. However, one of the most exciting aspects of Java is that it is easy to load classes from the network, not just locally.
For example, the browser can load classes through a custom class loader. There are also many ways to load classes. In addition to a simple local or network, you can also customize one of the most exciting aspects of Java:

* The digital signature is automatically verified before the untrusted code is executed.
* Decryption code based on the password provided by the user
* Dynamically create a class based on your needs

Everything you care about can be easily integrated into your application in the form of bytecode

 

Example of a custom Class Loader
If you have used appletviewer in JDK (Java software development kit) or other Java Embedded browsers, you have used the custom class loader. When sun just released the Java language, one of the most exciting things was to watch how Java executes code downloaded from a remote website. It looks incredible to execute the bytecode sent from a remote site over an HTTP connection. This is because Java has the ability to install a custom class loader. The browser of a small application contains a class loader. This Class Loader accesses a remote server instead of looking for a Java class locally and loads the original bytecode file through HTTP, then, it is converted into a Java class in the Java Virtual Machine. Of course, the classloaders also do many other things: they prevent insecure Java classes and keep different small programs on different pages from interfering with each other. A package written by Luke gorrie, Echidna, is an open Java software package that allows secure running of multiple Java applications in a Java virtual machine. It blocks interference between applications by using a custom classloader to copy class files to each application.

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.