Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of ClassLoader in JVM

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

JVM ClassLoader example analysis, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

Foreword:

Before we learn about JVM- class loading, let's take a look at why this is happening. Anyone who studies java knows that Java is a cross-platform language, and a set of code runs everywhere, so why can it be cross-platform?

To put it simply: the reason why it can run across platforms depends on the JVM of different platforms, we write code, javac compiles into .class files, and then through the class loader to JVM

In memory, then the Java interpreter translates the bytecode file into machine code, executes and displays the results, and knows the causes and consequences, so let's see how this class loader works.

Describe some concepts for easy understanding

Bytecode file is a kind of intermediate code that has nothing to do with any specific machine environment and operating system environment. It is a kind of binary file, which is the object code file generated by Java source file compiled by Java compiler. Neither the programmer nor the computer can read the bytecode file directly, and it must be interpreted and executed by a special Java interpreter, so Java is a language that interprets and runs on the basis of compilation.

The Java interpreter is responsible for translating bytecode files into machine code under specific hardware environment and operating system platform for execution. Therefore, the Java program cannot run directly on the existing operating system platform, it must run on the software platform known as the Java virtual machine.

The complete process is as follows. The picture comes from the Internet.

After getting a general picture, let's go on to learn the details!

1. What is class loading?

Generally speaking, it loads the compiled. Class into memory, and after being processed by the class loader, it can be directly used by the virtual machine.

Data, after the .class file class is loaded, a meta-information object describing the Class structure is formed in jvm, through which you can know

Class structure information, such as constructors, properties and methods, etc. Java allows users to use this Class-related meta-information object

Call the function of the Class object indirectly, and here is the Class class that we often see.

two。 Class loading process

After the class is loaded, the result is shown in the figure

3. Class loader

The Java class loader is part of the Java runtime environment that dynamically loads Java classes into the Java virtual machine. Because of the classloader, the Java runtime system does not need to know about files and file systems.

The Java class is not loaded into memory all at once, but when the application needs it. At this point, JRE will call Java ClassLoader, and these ClassLoader will dynamically load the class into memory.

Not all classes are loaded by a single ClassLoader. Based on the type of the class and the path of the class, it is decided to load the ClassLoader for that particular class. To understand the ClassLoader that loads the class, use the getClassLoader () method. All classes are loaded based on their names, and if none of these classes are found, it returns NoClassDefFoundError or ClassNotFoundException.

1. There are three types of Java Classloader:

1.BootStrap classloader: the Bootstrap classloader is machine code that starts the operation when JVM calls it. It is not a Java class. Its job is to load the first pure Java ClassLoader. Bootstrap ClassLoader loads classes from the rt.jar location. Bootstrap ClassLoader does not have any parent ClassLoader. Also known as Primodial ClassLoader.

two。 Extension ClassLoader: the extension ClassLoader is a child of Bootstrap ClassLoader and loads the extension of the core Java class from the corresponding JDK extension library. It loads files from the jre / lib / ext directory or any other directory pointed to by the system property java.ext.dirs.

3. System class loader: an application class loader is also called a system class loader. It loads the application type class found in the command line option of the environment variable CLASSPATH,-classpath or-cp. Application ClassLoader is a subclass of Extension ClassLoader.

Note: the ClassLoader delegate hierarchy model always runs in the order of Application ClassLoader- > Extension ClassLoader- > Bootstrap ClassLoader. Always give Bootstrap ClassLoader a higher priority, followed by Extension ClassLoader, then Application ClassLoader.

2 Java ClassLoader has three principles of functionality, they are:

1. Delegate model: the Java virtual machine and Java ClassLoader load classes into the Java file using an algorithm called the delegate hierarchy algorithm.

ClassLoader works based on a set of operations provided by the delegate model. They are:

ClassLoader always follows the principle of delegate hierarchy.

Whenever JVM encounters a class, it checks to see if the class is loaded.

If the class is already loaded in the method area, JVM continues execution.

If the class is not in the method area, JVM requires the Java ClassLoader subsystem to load that particular class, and then the ClassLoader subsystem hands over the control to Application ClassLoader.

The application ClassLoader then delegates the request to Extension ClassLoader, and Extension ClassLoader in turn delegates the request to Bootstrap ClassLoader.

Bootstrap ClassLoader will search in the Bootstrap classpath (JDK / JRE / LIB). If the class is available, load it, otherwise delegate the request to Extension ClassLoader.

Extension ClassLoader searches for classes in the extended classpath (JDK / JRE / LIB / EXT). If the class is available, load it, otherwise delegate the request to Application ClassLoader.

Application ClassLoader searches for the class in the application classpath. If the class is available, it is loaded; otherwise, a ClassNotFoundException exception is generated.

two。 Visibility principle: the visibility principle states that classes loaded by the parent ClassLoader are visible to the child ClassLoader, but classes loaded by the child ClassLoader are not visible to the parent ClassLoader. Assuming that the extension class loader has loaded the GEEKS.class class, the class is visible only to the extension class loader and the application class loader, but not to the boot class loader. If you try to load the class using Bootstrap ClassLoader again, it will give you an exception java.lang.ClassNotFoundException.

3.Uniqueness attribute: the Uniquesness attribute ensures that the class is unique and does not repeat the class. This also ensures that classes loaded by the parent class loader are not loaded by the child class loader. If the parent class loader cannot find the class, only the current instance will try to do so.

Java.lang.ClassLoader 's method

After the JVM requests the class, a few steps are followed to load a class. Classes are loaded according to the delegate model, but there are some important methods or functions that play a vital role in loading classes.

LoadClass (String name,boolean resolve): this method is used to load classes referenced by JVM. It takes the name of the class as a parameter. Type is loadClass (String,boolean).

The defineClass (): defineClass () method is the final method and cannot be overridden. This method is used to define a byte array as an instance of class. If the class is not valid, ClassFormatError is thrown.

FindClass (String name): this method is used to find the specified class. This method only finds but does not load classes.

FindLoadedClass (String name): this method is used to verify that the Class referenced by JVM has been previously loaded.

Class.forName (String name,boolean initialize,ClassLoader loader): this method is used to load and initialize classes. This method also provides the option to select any ClassLoader. If the ClassLoader parameter is NULL, Bootstrap ClassLoader is used.

Example: execute the following code before loading the class:

Protected synchronized ClassloadClass (String name, boolean resolve) throws ClassNotFoundException {Class c = findLoadedClass (name); try {if (c = = NULL) {if (parent! = NULL) {c = parent.loadClass (name, false);} else {c = findBootstrapClass0 (name);}} catch (ClassNotFoundException e) {System.out.println (e);}

Note: if a class is already loaded, it will return it. Otherwise, it delegates the search for the new class to the parent class loader. If the parent class loader cannot find the class, loadClass () calls the method findClass () to find and load the class.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report