Asp.net+android+ios Development, Net training, look forward to communicating with you!
Read the book "Deep Java Virtual machine"
Catalog: 1、java.lang.ClassLoader
Class Introduction 2, Class loader structure 3, load class procedure 4, custom class loader
The ClassLoader is responsible for loading the Java class's byte code into the Java virtual machine.
The ClassLoader is an innovation in the Java language and one of the most important reasons for the popularity of the Java language. It enables Java classes to be dynamically loaded into a Java virtual machine and executed.
Java Virtual machines Use Java classes in the following ways: Java source programs (. java files) are converted to Java bytecode (. class files) after they are compiled by the Java compiler. The ClassLoader is responsible for reading the Java byte code and converting it into java.lang.Class
an instance of the class. Each such instance is used to represent a Java class. newInstance()
an object of the class can be created by means of this instance. The actual situation can be more complex, such as the Java byte code may be generated dynamically through the tool, or it may be downloaded over the network.
Basically all ClassLoader are java.lang.ClassLoader
an instance of the class.
1、java.lang.ClassLoader
Class Introduction
java.lang.ClassLoader
The basic function of a class is to find or generate its corresponding byte code based on the name of a specified class, and then define a Java class from those byte codes, which is java.lang.Class
an instance of the class.
2. Structure of class Loader
The ClassLoader in Java can be broadly divided into two categories, one for the system, the other for Java application developers. There are three main types of ClassLoader available in the system:
- Boot class loader (bootstrap class loader): It is used to load Java's core library, which is implemented by native code and does not inherit from
java.lang.ClassLoader
.
- Extension class loader (Extensions class loader): It is used to load the Java extension library. The implementation of the Java virtual machine provides an extension library directory. The ClassLoader finds and loads the Java class in this directory.
- System class Loader: It loads Java classes according to the Classpath (CLASSPATH) of the Java application. In general, Java-applied classes are loaded by it. You can
ClassLoader.getSystemClassLoader()
get it by.
In addition to the system-provided ClassLoader, developers can java.lang.ClassLoader
implement their own ClassLoader by inheriting classes to meet some special requirements.
Code to validate this structure
1 Public classClassloadertree {2 3 Public Static voidMain (string[] args) {
Creating the class loader4ClassLoader loader = Classloadertree.class. getClassLoader ();
Print the parent class load through the while loop5 while(Loader! =NULL) { 6 System.out.println (loader.tostring ());7Loader =loader.getparent ();8 } 9 } Ten}
How a 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 loader loading this class is the same. The two classes are the same only if they are the same.
3. The process of loading classes
In the previous introduction of the class loader's proxy mode, it was mentioned that the class loader would first delegate to other ClassLoader to try to load a class. This means that the class loader that actually completes the loading of the class and the ClassLoader that starts the loading process may not be the same. The loading of a really complete class is implemented by invocation, defineClass
while the loading process of the startup class is implemented by invocation loadClass
. The former is called the definition loader for a class (defining loader), which is called the initial loader (initiating loader). When a Java virtual machine determines whether two classes are the same, the class's definition loader is used. That is, it is not important which class loader initiates the loading of the class, it is important to finally define the loader for this class. The correlation between the two kinds of loaders is that the definition loader for a class is the initial loader for the other classes it references. If com.example.Outer
a class refers to a class, the class's com.example.Inner
com.example.Outer
definition Loader is responsible for initiating com.example.Inner
the class's loading process.
Method loadClass()
throws an java.lang.ClassNotFoundException
exception, defineClass()
and the method throws an java.lang.NoClassDefFoundError
exception.
After the class loader successfully loads a class, the instance of the resulting java.lang.Class
class is cached. The next time the class is requested to load, the ClassLoader will use the instance of the cached class directly without attempting to load it again.
4. Custom class loader
Although in most cases, the class loader implementation provided by default can already meet the requirements. But in some cases, you still need to develop your own ClassLoader for your app. For example, your application transmits byte codes for Java classes over the network, and for security purposes, these byte codes are encrypted. At this point you will need your own classloader to read the encrypted byte code from a network address, then decrypt and validate, and finally define the classes to run in the Java virtual machine.
Customizing a file System ClassLoader code
1 Public classFilesystemclassloaderextendsClassLoader {2 3 PrivateString RootDir;4How loaders are constructed5 PublicFilesystemclassloader (String rootdir) {6 This. RootDir =RootDir;7 } 8Find a class9 protectedClass<?> Findclass (String name)throwsClassNotFoundException {Ten byte[] Classdata =getclassdata (name); One if(Classdata = =NULL) { A Throw Newclassnotfoundexception (); - } - Else { the returnDefineClass (name, Classdata, 0, classdata.length); - } - } -Obtaining data in a class + Private byte[] Getclassdata (String className) { -String Path =Classnametopath (className); + Try { AInputStream ins =NewFileInputStream (path); atBytearrayoutputstream BAOs =NewBytearrayoutputstream (); - intBufferSize = 4096; - byte[] buffer =New byte[buffersize]; - intBytesnumread = 0; - while((Bytesnumread = ins.read (buffer))! =-1) { -Baos.write (buffer, 0, Bytesnumread); in } - returnBaos.tobytearray (); to}Catch(IOException e) { + e.printstacktrace (); - } the return NULL; * } $Defining the class load pathPanax Notoginseng Privatestring Classnametopath (String className) { - returnRootDir +File.separatorchar the+ classname.replace ('. ', File.separatorchar) + ". Class"; + } A}
Asp.net+android+ios Development, Net training, look forward to communicating with you!
Black Horse Programmer Series Nineth class loader