Home  >  Article  >  Java  >  How to use endorsed in Java to override instances of classes provided by jdk

How to use endorsed in Java to override instances of classes provided by jdk

黄舟
黄舟Original
2017-09-05 10:06:191911browse

This article mainly introduces to you the relevant information about how Java uses endorsed to cover the classes provided by jdk. The article introduces it in detail through sample code. It has certain reference learning value for everyone's study or work. Friends in need Let’s learn together with the editor below.

Preface

When we analyzed the principle analysis of Tomcat catalina.bat before, we found that there is - in the parameters for starting tomcat. Djava.endorsed.dirs Parameters

are as shown below:


##-Djava.endorsed. dirs##Java provides endorsed technology:


About endorsed: It can be simply understood as

-Djava.endorsed.dirs

specified The jar file placed in the directory will have the function of covering the system API. However, the classes that can be covered are limited, which does not include classes in the java.lang package (for security reasons). Why must we use endorsed to replace classes in jdk?

Because Java uses the parent delegation mechanism to load classes. The classes provided by jdk can only be loaded by the class loader Bootstrap. If you want to replace a certain class in jdk in your application, it is impossible to do so, so java provides endorsed to achieve the class you want to replace in the system.

ExampleModify

get(int index) ## in the

java.util.ArrayList class #Method, add the output value information obtained in this method. If it is a string, it will be output directly. If not, the class information will be output, and the classloader of the element class will be loaded. The code is as follows:

# Then package the jar file and put it in a directory.

I put it here in the directory "D:\endorsed"

When the test class

public class Bootstrap {
 public static void main(String[] args) {
  ArrayList<String> list = new ArrayList<String>(10);
  for(int i=0;i<10; i++){
   list.add("test"+i);
   list.get(i);
  }
 }
}

is run Add the
-Djava.endorsed.dirs=D:\endorsed

parameter. As shown below:

Running results

From the results we found that the printed information is not our program What is output in is what is printed in the ArrayList.get()

method.

endorsed Another modification method

According to the official document description: If you don’t want to add the -D parameter, if we want to base it on this JDK If all are changed uniformly, then we can put the jar we modified into:


$JAVA_HOME/jre/lib/endorsed

In this way, all ArrayLists based on this JDK have changed! ! !

Summarize

The above is the detailed content of How to use endorsed in Java to override instances of classes provided by jdk. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn