Analysis Java ClassLoader and ClassLoader (ii): Classpath and find the order of class bytecode, analyze Extclassloader and Appclassloader source code

Source: Internet
Author: User

Let's go back to classpath.

The role of Classpath:

The function of classpath is to specify the path to the lookup class: When a class is executed using Java commands (the main method in the class), the class is looked up from the classpath.

specify a way to classpath one:
Set the environment variable classpath, separated by semicolons between multiple paths, or as a jar package path.
Example: Classpath=c:/myclasses/;c/mylib/aa.jar;c:/mylib/bb.jar;.
Note: Case is not case-sensitive in windows, so the specified environment variable is named Classpath or classpath.

Specify the way classpath two:
specified by the-classpath parameter when executing Java commands.
Example: Java-classpath C:/myclasses/;c:/mylib/aa.jar cn.itcast.MainApp
Note: This will only use this parameter to specify the CLASSPATH, can not find the class error, will not use the CLASSPATH environment variable!

specify the order of each path when classpath:
The conclusion of the experiment is to look in the previous path in the order specified in Classpath, and if not found, look in the next path until the class bytecode is found or the Noclassdeffounderror is reported.
another way to specify a classpath:
The class bytecode file is made into a jar package and placed in the lib/ext/directory of the JRE so that it can be found directly at execution time without the need to specify CLASSPATH.

Class Loaders and Classpath

From the previous article we learned that the ClassLoader uses three by default:
1,bootstrap ClassLoader, starting the ClassLoader, is responsible for loading the core class (that is, the class at the beginning of all java.*).
2,extension ClassLoader, the extension classloader, is responsible for loading the class in the jar package stored in the lib/ext/directory of the JRE.
3,application ClassLoader, the application ClassLoader, is responsible for loading the classes of the application itself (the class in the Classpath directory).

Analysis Extclassloader

Extension ClassLoader is responsible for loading the classes in the extension classpath (the default is the lib/ext/directory of the JRE) , which means that if you put the jar package in this directory, you can directly use the class bytecode inside without specifying the CLASSPATH! Note that this requires you to put the jar package in this directory and put the. class file directly in this directory. Analyze the source code (Sun.misc.launcher$extclassloader Class):

StaticClassExtclassloaderExtendsURLClassLoader { Private file[] dirs; Look at this method first. PublicStatic Extclassloader Getextclassloader ()Throws IOException {  Call the Getextdirs () method to get the extended class path of the configuration  Final file[] dirs = Getextdirs ();  try {   Use the path returned by the Getextdirs () method to generate a new ClassLoader instance   Return (Extclassloader) accesscontroller.doprivileged (New Privilegedexceptionaction () {    Public Object Run ()Throws IOException {     int len = dirs.length;     for (int i =0; i < Len; i++) {     Metaindex.registerdirectory (Dirs[i]);    }     ReturnNew Extclassloader (dirs);   }  }); }catch (Java.security.PrivilegedActionException e) {   Throw (IOException) e.getexception (); }} Look at this method again. PrivateStatic file[] Getextdirs () {  Get the extended classpath for the configuration String s = System.getproperty ("Java.ext.dirs"); File[] dirs;  if (s! =NULL) {  StringTokenizer st =New StringTokenizer (S, file.pathseparator);   int count = St.counttokens ();  Dirs =New File[count];   for (int i = 0; i < count; i+ +) {    Dirs[i] = new File (St.nexttoken ());   } } else {   Dirs = new file[0]; }  return dirs;}  //other code slightly  ...}   

Application ClassLoader is responsible for loading the class in the Classpath directory , which means that classpath is for this class loader. Analyze the source code (Sun.misc.launcher$appclassloader Class):

StaticClassAppclassloaderExtendsURLClassLoader { PublicStatic ClassLoader Getappclassloader (Final ClassLoader EXTCL)Throws IOException {  Get the configured classpath path  Note 1: You can change the value of Java.class.path by setting the CLASSPATH environment variable.  Note 2: You can also use System.setproperty ("Java.class.path", "NewPath") in your program to change the value of Java.class.path.  Final String s = System.getproperty ("Java.class.path");  Final file[] Path = (s = =NULL)?New file[0]: Getclasspath (s);  Use the path in Classpath to generate a new ClassLoader instance and return  Return (Appclassloader) accesscontroller.doprivileged (New Privilegedaction () {public   Object Run () {   url[] URLs = (s = = null)? 
                                                 
                                                  new url[
                                                  0]: pathtourls (path);     return new Appclassloader (URLs, EXTCL);    }  }); } //other code slightly ...}              
                                                 

When Appclassloader meets Extclassloader,

What if there is a class in the jar package in the lib/ext/directory of the JRE, and we have this class in the classpath we specify? Think of the strategy of class loading find class bytecode! The conclusion is that the classes in Lib/ext/xx.jar will be executed! because the class loader uses delegate mode for class loading, and Extclassloader is the ancestor of Appclassloader, Appclassloader will let Extclassloader load first. If the parent's classloader is not found, it will not load itself.

Conclusion:
1, put the jar package into the extension classpath to directly use the class (the default is the lib/ext/directory of the JRE)
The 2,classpath is configured for Appclassloader.
3, if there is a class in the extension classpath, and there is a class in Classpath, the class in the extended Classpath will be used.

Http://www.tuicool.com/articles/bQFnqmi

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.