Your location:Tech News>News>Script>_ classloader class loader loading java classes based Detailed

Latest News

_ classloader class loader loading java classes based Detailed

Author:fsadmin

Views:

  Basic concepts

  Classloader class loader used to load Java classes into the Java Virtual Machine.The difference is that with the ordinary procedure.Java program (class file) is not a native executable program.When running a Java program, the first run the JVM (Java Virtual Machine), and then load the Java class into the JVM running inside, partly responsible for loading Java class is called the Class Loader.

  JVM itself contains a ClassLoader called Bootstrap ClassLoader, and the same JVM, BootstrapClassLoader with native code implementation, it is responsible for loading the core JavaClass (that is, all java.* The beginning of the class).In addition JVM will provide two ClassLoader, they are written in the Java language, loaded by BootstrapClassLoader; Extension ClassLoader which is responsible for loading the extended Javaclass (for example, all javax.* The beginning of the class and stored in the ext directory of the JRE class), ApplicationClassLoader responsible for loading the application itself class.

  When running a program, JVM startup, operation bootstrapclassloader, load the ClassLoader java core API (ExtClassLoader and AppClassLoader be loaded at this time), and then calls ExtClassLoader load the extension API, and finally loaded Class AppClassLoader defined under CLASSPATH directory, which is a basic program loading process.

  Note: The learning program application ClassLoader see OSGI

  When JVM ClassLoader will use it to load a class?When you use java to execute a class, JVM load the class using ApplicationClassLoader; then if the class A reference to the class B, either directly or by reference to Class.the forName () references, the JVM will find ClassLoader loading of class A, and uses this to load class B ClassLoader.According to the effective implementation of the statement JVM run-time to determine the need to load new classes to load as few classes, and this class is not the same compiler.

  Why use your own ClassLoader?

  JVM ClassLoader itself seems to have been enough, why do we need to do to create your own ClassLoader?

  Because the JVM ClassLoader only know how to load comes from the local file system standard java class files, if you write your own ClassLoader, you can do:

  1) untrusted code prior to execution, automatic verification of the digital signature

  2) to dynamically create customized in line with the specific needs of the user to build class

  3) get java class from a specific place, such as a database

  4) etc.

  In fact when using Applet, it uses a specific ClassLoader, because when you need to load java class from the network, and to check the relevant safety information.

  Most application servers using the ClassLoader technology, even if you do not need to create your own ClassLoader, understand its principles will also help to better deploy their own applications.

  Key note: In fact, an already loaded class can not be updated, if you try to load again the same ClassLoader same class, get an exception (java.lang.LinkageError: duplicate classdefinition), we are only able to re-create a new ClassLoader to load a new instance of the class again.As for the already loaded class, developers do not have to go to hell, because there may be instances it is being used, to the extent relevant examples have been recovered memory, the JVM will at the appropriate time the class will not be used to uninstall.

  Most of the program will use three kinds of Java class loader provided by the system

  1.Start class loader (Bootstrap ClassLoader), responsible for the existence of\ Lib directory, or -Xbootclasspath parameters specified path, and is recognized by the virtual machine to virtual machine class library loaded.Note that by file name recognition, such as rt.jar, even if the name does not comply with the library in the lib directory will not be loaded.

  2.Extension class loader (ExtClassLoader), which is responsible for\ Lib \ ext directory, or java.ext.dirs system variables path specified all libraries.

  3.Application class loader (App-ClassLoader), by cassLoader.getSystemClassLoader () Gets, which is responsible for loading the user class path (the ClassPath) designated library, in general this is the default application class loader.

  Parents entrusted mode is recommended for Java developers designers a class loader implementation.Parent delegation model work process: If a class loader loads the class received a request, it does not own to try to load this class, but to delegate this request to the parent class loader to complete.Each level class loader is true, so all requests will eventually be transferred load to the top of the boot class loader, only when the parent class loader required class is not found, the child will try themselves loader load.Parents are the benefits of Java class model with its class loader together with a prioritized hierarchy.For example class Object, stored in rt.jar, regardless of which class loader loads the class, are ultimately assigned to the model in the top of the boot class loader, so the Object class loader using multiple types of environments to ensure that the program is still the same class.Conversely, if the parents are not using the model, it is possible to write a user''s own Object class, leading to a number of different systems appeared Object class, so the Java type system, the most basic of behavior will not be guaranteed.

  Above this classloader class loader _ based on loading java class explain that small series to share the entire contents of everyone, and I hope to give you a reference, I hope you will support script Home.

  You may also be interested in the article: Java Runtime Environment ClassLoader class loading mechanism Detailed Java Classloader mechanism uses code analysis in Java ClassLoader class loader learning summary Java classloader and namespace details java ClassLoader mechanism to explain in detail depth analysis of the operating mechanism of Java in Classloader ClassLoader Java class loader usage resolve

Recommend article

Relate article