One, class loader
1, what is the class loader
The class loader is the tool that loads the class, the first thing the Java Virtual machine JVM runs in the class is to load the byte code of the class.
The name of the class Loader tool class is positioned and the byte code data for the production class is returned to the JVM.
Java.lang.ClassLoader class
The class loader is the object that is responsible for loading the class. The ClassLoader class is an abstract class.
If the binary name of a class is given, the class loader attempts to find or generate data that constitutes a class definition.
The general policy is to convert the name to a file name and then read the "class file" of that name from the file system.
The class object of an array class is not created by the ClassLoader, but is created automatically by the Java runtime as needed.
The class loader for an array class is returned by Class.getclassloader (), which is loaded with the class of its element type
Is the same, and if the element type is a base type, the array class does not have a class loader.
Construction method
Protected ClassLoader () creates a new ClassLoader using the ClassLoader returned by Getsystemclassloader (), which is used as the parent class loader.
Protected ClassLoader (ClassLoader parent) creates a new class loader using the specified parent class loader for the delegate operation.
Related methods
getClassLoader () returns the class loader for the class. return type ClassLoader
GetParent () returns the parent class loader for the class loader.
LoadClass (String name) loads a class named name, and the result returned is an instance of the Java.lang.Class class.
Findclass (String name) looks for 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 name, and the result returned is an instance of the Java.lang.Class class.
DefineClass (String name, byte[] B, int off, int len) converts the contents of byte array B into a Java class, and the result returned is an instance of the Java.lang.Class class. This method is declared final.
Resolveclass (class<?> c) links the specified Java class. 2, the class loader tree-like structure
Class Loaders in Java can be roughly divided into two classes: one is provided by the system and the other is written by the programmer.
The system provides a class loader that consists of three of the following:
Boot class loader (bootstrap class loader): The core library that is used to load Java, is implemented using native code
Extended class loader (Extensions class loader): An extension library that is used to load Java.
System class Loader Loader: Java classes are loaded according to the classpath classpath of Java applications.
In general, Java-applied classes are loaded by the System class loader.
It can be obtained by Classloader.getsystemclassloader ().
In addition to the class loader provided by the system, you can implement your own ClassLoader by inheriting the Java.lang.ClassLoader class.
All Class loaders have a parent class loader, which can be obtained by means of the GetParent () method.
The parent class loader of the System class loader is the extended class loader, while 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 loader is the class loader that loads the Java class for this class of loader.
Because the class loader Java class, like any other Java class, is loaded by the ClassLoader. Generally speaking
The parent class loader of the class loader written by the developer is the System class loader. Class loaders are organized in this way,
Form a tree-like structure. The root node of the tree is the boot class loader.
Multiple class loaders can be installed in the Java Virtual machine, and the system defaults to three main class loaders.
Bootstrap,extclassloader,appclassloader, each class is responsible for loading the characteristic location of the class.
The class loader is also a Java class, because the class loader of the Java class is also loaded by the ClassLoader itself,
So there must be a first class loader, it's bootstrap.
All class loaders In the Java Virtual machine are organized with a parent-child relationship tree structure, and when each class loader is instantiated,
The System class loader needs to be loaded for its parent class either by specifying a parent class loader object or by default.
Instance: getting the class loader[Java] View plain copy public class classloadertest { public static void main (String[] args) { //returns the full name of the class system.out.println ( Classloadertest.class); //returns the entity represented by this Class object in the form of String (class, interface, array class, Base type or void) name. system.out.println (ClassLoaderTest.class.getName () ); // returns the short name of the underlying class given in the source code. system.out.println ( ClassLoaderTest.class.getSimpleName ()); //returns the class loader for the class. system.out.println ( ClassLoaderTest.class.getClassLoader ()); //returns the class loader's byte code object for this class system.out.println (ClassLoaderTest.class.getClassLoader (). GetClass ()); // Returns the name of the bytecode object of the class loader for this class system.out.println ( ClassLoaderTest.class.getClassLoader (). GetClass (). GetName ()); //returns the loader for the system class--No system.out.println (SYSTEM.CLASS.GEtclassloader ()); } } /* results class com.wgxin.classloadertest com.wgxin.classloadertest classloadertest sun.misc.launcher$appclassloader@43be2d65 class sun.misc.launcher$appclassloader sun.misc.launcher$appclassloader null */ 3, the custom class loader's writing principle
Analysis:
Although in most cases, the default ClassLoader implementations provided by the system already meet the requirements, in some cases you will need to develop your own classloader for the application. For example, your application transmits the byte code of the Java class over the network, and the byte code is encrypted to ensure security. This time you will need your own classloader to read the encrypted byte code from a network address, then decrypt and verify, and finally define the classes to run in the Java virtual machine. The following two concrete examples illustrate the development of class loaders.
File System class loader, the first classloader to load Java byte code stored on the file system [Java] View plain copy//Inheritance loading carrier ClassLoader class public class filesystemclassloader extends classloader{ private String rootDir; //Constructor public filesystemclassloader (string rootdir) { this.rootDir = rootDir; } //defines a protected function Protected class<?> findclass (String name) throws classnotfoundexception { //Invoke Custom Getclassdata methods byte