Java Core Learning (37) class loader

Source: Internet
Author: User

The ClassLoader is responsible for loading the. class file (possibly on disk, possibly on the network) into memory and generating the corresponding Java.lang.class object for it.

Introduction of Class Loader

Once a class is loaded by the ClassLoader to generate an java.lang.class instance in memory, the same class is no longer loaded.

Unique identity of the class: In the Java language, a class is uniquely identified with its fully qualified name, but in the JVM, a class is uniquely identified with its fully qualified name and its class builder

When the JVM starts, it produces an initial class loader hierarchy of three ClassLoader, respectively:

1, Bootstrap ClassLoader: The root class loader, also known as the Boot class loader, is responsible for loading the Java core class. So what are the so-called core classes? If you are using Sun Company's JAVAJDK, you can output the core class library loaded by the root loader using the following code.

  

Compile and run, you can see results similar to the following:

  

You can also specify that the root loader of the JVM load additional classes, and when using Java commands, use the-xbootclasspath option or specify the Sun.boot.class.path system property value by using the-D option to specify that additional classes are loaded.

The Genga carrier is not implemented in Java.

2. Extension Classloader, known as the extension ClassLoader, is responsible for loading the class of jar packages of the JRE's extended directory (%java_home%/jre/lib/ext or the directory specified by the Java.ext.dirs system properties).

In this way, you can extend functionality outside of the core class for Java,

3. The system ClassLoader is called the Systems (application) class loader and is responsible for loading the-CLASSPATH option that loads from Java commands when the JVM starts, Java.class.path the System property, or the jar package or classpath specified by the CLASSPATH environment variable.

Second, class loading mechanism

There are three types of JVM class loading mechanisms:

Overall responsibility: When a classloader is responsible for loading a class, other classes that the class relies on and references are also loaded by the ClassLoader, unless the display is loaded using a different loader.

Parent class Delegate: Let the parent loader attempt to load the class before attempting to load it from its own classpath only if the parent class loader cannot load it.

Caching mechanism: The caching mechanism will ensure that all loaded class objects are cached, and when a class is required in the program, the ClassLoader first searches for the class from the cache, and the binary data for that class is not read when there is no such object in the cache. and translates it into a class object, so modifying the class file will have to restart the JVM.

ClassNotFoundException is thrown when the class loader cannot find a class in the cache and cannot find a class file under the ClassLoader path.

Third, create your own class loader

Programmers can implement their own class loaders by inheriting the ClassLoader class, and then rewrite the Findclass () method.

In general, you can use your own class loader to achieve some of the following features:

Automatically validates the digital signature before executing the code.

According to the user-provided password decryption code, implement code obfuscation to avoid the anti-compilation class file;

Dynamically loading classes according to user requirements;

The other data is loaded into the application in bytecode form according to the application requirement.

Four, URLClassLoader class

The class is also the parent class of the system ClassLoader and extension class loader, which can load classes from local files or get binaries from remote hosts to load classes Buchholz

  

The code above does not need to add the MySQL driver into the classpath to connect to the database. File: The URL that represents the local file system can also be prefixed with HTTP, ftp.

Java Core Learning (37) class 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.