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

How to implement a class loader in Java

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

Share

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

How to implement a class loader in Java? in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

In fact, there are many kinds of class loaders in virtual machines, but they are mainly divided into the following types:

Start the classloader

The main function of starting the classloader is to load all the class libraries in the JAVA_HOME/lib directory. But there is a prerequisite when it loads. This means that the virtual machine recognizes the load by the specified file name, such as rt.jar. If the name does not match, the class loader will not load even if it is placed in the lib directory to start the class loader. The startup class loader cannot be directly referenced by Java programs because it is implemented in the C++ language.

Extended class loader

The function of the extension class loader is to load all the class libraries in the JAVA_HOME/lib/ext directory. Because the extension class loader is implemented in the Java language itself, users can use the extension class loader directly.

Application class loader

The function of the application class loader is to load the class libraries specified on the user classpath (ClassPath). Users can also use the application class loader directly. An application class loader can be obtained in the following ways.

ClassLoader.getSystemClassLoader ()

If there is no custom class loader in the program, the application class loader is the default class loader for the program.

Custom class loader

As the name implies, the custom class loader is the class loader developed by the user. Some API is provided in Java API to help us develop our own class loader.

Parental delegation model

As we mentioned above, the parent delegation model is very important in the classloader, so what exactly is the parent delegation model? In fact, there is a hierarchical relationship between the class loader and the class loader in the above class loaders, which is called the parent delegation model. Let's take a look at the picture below and then make a specific explanation.

It is specified in the classloader that all classloaders, except the top-level startup classloader, must have their own parent class loader. As shown in the figure, the parent class loader of the custom class loader is the application class loader, the parent class loader of the application class loader is the extension class loader, and the parent class loader of the extension class loader is the startup class loader. So what's the purpose of doing this? Why should there be such a hierarchical relationship?

Let's take a look at the workflow of the parent delegation model. If a class loader receives a load request from a class, it will not load the class first, but delegate the request to the parent class loader, and every class loader will do so. so no matter which class is loaded will eventually be delegated to the initiating class loader to load, because of the above delegate rules. The subclass loader tries to load the class itself only if the parent class loader cannot load the class (the class loader did not search for the class within its own scope). Although we know the workflow of the above class loader, that is, the parent delegation model, how important is it to Java applications? Let's give a simple example to illustrate the benefits of the parental delegation model.

In parent delegation mode, starting the class loader can load the class before extending the class loader. The extension class loader can load the class before the application class loader. The application class loader can load the class before the custom class loader. So in the parent-child delegation chain, launching the classloader is the most trusted-the core Java API checks each loaded class, and then goes to the extended classloader, the application classloader, and the custom classloader. What if the custom class loader tries to load a java.lang.Virus (virus class)?

According to the parental delegation model. This class is always delegated to the startup classloader to load the class, because the startup classloader is the core Java API. In the java.lang package, there is no such class, so it cannot be loaded. According to the parent delegation model, when the parent class loader cannot be loaded, the subclass loader attempts to load. And so on, other class loaders cannot load this class either. The class is loaded by a user-defined class loader. If the custom class loader successfully loads the class. Because Java allows access to each other in the same package, this java.lang.Virus class allows access to all classes under the java.lang package, and can use this special access for ulterior purposes. This shows to be a very unsafe operation. So how does the classloader ensure the security of the Java program?

In addition to some of the above features, the classloader also has other features to ensure that this code does not have access to other classes in the java.lang package. How is the Java virtual machine implemented?

Because only special permissions to access each other are granted in the Java virtual machine to types loaded into the same package by the same class loader. So the classes in the java.lang package are loaded by the startup class loader, while the java.lang.Virus is loaded by the custom class loader. So these types do not belong to the same runtime package.

Runtime package: a collection of multiple types that are loaded by the same class loader and belong to the same package.

Before the Java virtual machine allows two classes to belong to the same package for access, the Java virtual machine must not only determine that they belong to the same package, but must also confirm that they belong to the same runtime package (which must be loaded by the same class loader). Therefore, the java.lang.Virus class cannot access the permissions under the java.lang package. This ensures the safe operation of the Java program.

This is the answer to the question about how to implement a class loader in Java. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

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