Java Class Loader

Source: Internet
Author: User
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.ListingThe following is an example of a user-defined class loader that can be run.

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.