The role of the Java ClassLoader is to load classes at run time. The Java ClassLoader is based on three mechanisms: delegation, visibility and uniqueness. The delegate mechanism refers to the request to the parent class loader to load a class, and if the parent loader is not able to find or load the class, then load it. The principle of visibility is that the classloader of the subclass can see all the classes loaded by the parent ClassLoader, and the parent ClassLoader does not see the classes loaded by the child ClassLoader. The principle of uniqueness is to load only one class at a time, which is determined by the delegation mechanism to ensure that the subclass loader does not load the class loaded by the parent ClassLoader again. A proper understanding of the ClassLoader can help you solve noclassdeffounderror and java.lang.ClassNotFoundException because they are related to the loading of classes. The ClassLoader is often an important question in a more advanced Java interview, and the Java ClassLoader and how it works and how Classpath works are often asked. The question of whether a class can be loaded by two different ClassLoader is often seen in Java interview questions. In this tutorial, we will learn what the ClassLoader is, how it works, and some knowledge points about the ClassLoader.
What is a class loader
The class loader is a class that is used to load class files. The Java source code is compiled into a class file by the Javac compiler. The JVM then executes the code in the class file to execute the program. The class loader is responsible for loading the file system, network, or other source class files. There are three default class loaders: The bootstrap class loader, the extension class loader, and the System class loader (or called the application ClassLoader). Each type of loader has a set of where to load the class.
- · The Bootstrap class loader is responsible for loading the JDK class file in the Rt.jar, which is the parent loader for all class loaders. The Bootstrap class loader does not have any parent classloader, and if you call String.class.getClassLoader (), it returns NULL, and any code based on this will throw a NullPointerException exception. The bootstrap loader is called the initial class loader.
- · and extension the request to load the class first to its parent loader, that is, bootstrap, if it does not load successfully, then from the Jre/lib/ext directory or java.ext.dirs system properties defined under the directory load class. The extension loader is implemented by Sun.misc.launcher$extclassloader.
- · The third default loader is the System class loader (also known as the Application class loader). It is responsible for loading some application-related classes from the CLASSPATH environment variables, which are typically defined by-classpath or-CP command-line options, or manifest classpath properties in the jar. The Application class loader is a sub-loader of the extension class loader. Implemented through Sun.misc.launcher$appclassloader.
Except for the Bootstrap class loader, which is mostly written by C, the other ClassLoader are implemented by Java.lang.ClassLoader.
To summarize, here are the three kinds of loader load class files where:
1) Bootstrap class loader –jre/lib/rt.jar
2) Extension class loader –jre/lib/ext or Java.ext.dirs-pointing directory
3) The Application class loader –classpath environment variables, defined by-classpath or-CP options, or manifest property definitions in the jar.
How the class loader works
As I mentioned before, the ClassLoader works based on three mechanisms: delegation, visibility, and uniqueness. In this section, let's take a closer look at these rules and use an example to understand how it works. The following shows how the ClassLoader works by using the delegate mechanism.
Delegation mechanism
When a class is loaded and initialized, the class is loaded only when it needs to be loaded. Assuming that you have an application that requires a class called Abc.class, the request to load the class first is delegated by the Application class loader to its parent ClassLoader extension class loader, and then to the Bootstrap class loader. The Bootstrap class loader will first see if there is no such class in Rt.jar, because there is no such class, so this request is returned by the Extension class loader, which will look at the Jre/lib/ext directory if there is no such class, if this class was found by the extension class loader , then it will be loaded, and the application class loader will not load this class, and if the class is not found by the extension ClassLoader, then the application class loader is looking for it from classpath. Remember that classpath defines the load directory for class files, and path is the execution path that defines executable programs such as Javac,java.
Mechanism of visibility
Depending on the visibility mechanism, the subclass loader can see the classes loaded by the parent ClassLoader, and vice versa. So in the example below, when Abc.class has been loaded by the application class loader, and if you want to load the class with the extension ClassLoader, you will throw a java.lang.ClassNotFoundException exception.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23 24 25 26 27 28 29 |
Package test; Import Java.util.logging.Level; Import Java.util.logging.Logger; /** * Java program to demonstrate what ClassLoader works in Java, * In particular about visibility principle of ClassLoader. * * @author Javin Paul */ public class Classloadertest { public static void Main (String args[]) { try { Printing ClassLoader of this class System.out.println ("Classloadertest.getclass (). getClassLoader ():" + ClassLoaderTest.class.getClassLoader ()); Trying to explicitly load this class again using Extension class loader Class.forName ("Test. Classloadertest ", True , ClassLoaderTest.class.getClassLoader (). GetParent ()); } catch (ClassNotFoundException ex) { Logger.getlogger (ClassLoaderTest.class.getName ()). log (Level.severe, NULL, ex); } } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Classloadertest.getclass (). getClassLoader (): [Email protected] 16/08/2012 2:43:48 AM Test. Classloadertest Main Severe:null Java.lang.ClassNotFoundException:test. Classloadertest At Java.net.urlclassloader$1.run (urlclassloader.java:202) At java.security.AccessController.doPrivileged (Native Method) At Java.net.URLClassLoader.findClass (urlclassloader.java:190) At Sun.misc.launcher$extclassloader.findclass (launcher.java:229) At Java.lang.ClassLoader.loadClass (classloader.java:306) At Java.lang.ClassLoader.loadClass (classloader.java:247) At JAVA.LANG.CLASS.FORNAME0 (Native Method) At Java.lang.Class.forName (class.java:247) At Test. Classloadertest.main (classloadertest.java:29) |
A single-sex mechanism
According to this mechanism, the class that the parent loader has loaded cannot be loaded by the quilt loader a second time. Although it is possible to override a classloader that violates delegates and singleness mechanisms, it is not advisable to do so. You should strictly abide by these three mechanisms when you write your own classloader.
How to explicitly load a class
Java provides an explicit load class of API:Class.forName (classname) and Class.forName (classname, initialized, ClassLoader). As in the example above, you can specify the name of the class loader and the name of the class to load. Class is loaded by calling the LoadClass () method of Java.lang.ClassLoader, and the LoadClass () method calls the Findclass () method to locate the byte code of the corresponding class. In this example, the extension class loader uses Java.net.URLClassLoader, which finds the class file from the jar and directory, and all lookup paths ending with "/" are considered directories. If Findclass () is not found then it throws an Java.lang.ClassNotFoundException exception, and if found, it calls DefineClass () converts the bytecode into a class instance and returns.
Where to use the class loader
The ClassLoader is a powerful concept and is used in many places. The most classic example is Appletclassloader, which is used to load classes used by applets, while applets is mostly used on the web, not on the local operating system. Using different class loaders, you can load the same class from different source addresses, which are considered different classes. The Java EE uses multiple ClassLoader to load classes in different places, such as war files loaded by the Web-app class loader, while classes in Ejb-jar are loaded by another class loader. Some servers also support hot deployment, which is also implemented by the ClassLoader. You can also use the ClassLoader to load data from databases or other persistent layers.
The above is about how the ClassLoader works. We already know the principles of delegation, visibility, and uniqueness, which are critical to debugging the ClassLoader-related issues. These are essential knowledge for Java programmers and architects alike.
How the
Java ClassLoader works