[Java Virtual Machine Series knowledge: sorting out 2] How Java Virtual Machine queries class files

Source: Internet
Author: User

Principles of searching for class files --
It is recommended that you compile and run in the command line window at the beginning, instead of using jcreator, eclipse, and other ide To help with those tasks. Try to do this yourself:
Javac-classpath yourpath *. Java
Java-classpath yourpath *. Class
Many people may understand that the purpose of setting classpath is to tell the compiler where to find your class file. however, at least I understand the principle of JVM to query classes today. The compiler must load classes by classloader. The classloader has three levels, from high to low, which are bootclassloader (the name may be inaccurate ), extclassloader, appclassloader.

These three loaders correspond to the compiler to find the class file priority and different paths: bootclassloader corresponds to the JRE/classes path, which is the place where the compiler first looks for the class
Extclassloader corresponds to the JRE/lib/EXT path, which is the place where the compiler first looks for the class.
Appclassloader corresponds to the current path, so it is also the place where the compiler finds the class by default.

In fact, you can writeProgramSimple test, for any class, such as,
Call New A (). getclass (). getclassloader (). tostring () to print it out. If you put the class file in a different path and execute it again, you will see the difference. Note that if it is printed as null, it indicates the highest level of bootclassloader, because it is written in C ++ and does not have the name of the Class Loader corresponding to Java.

The order of search is a kind of thought, that is, if this level cannot be found, it can only be found above this level, rather than looking down. However, it seems that the features from jdk1.4 to jdk1.6 have changed and no details are found. So we will not give an example. I would like to tell you that sun designed this system as a technology core Mr. Gong Li, a purebred Chinese! Pai_^

In this way, we hope that you will not be confused why class files cannot be found in the general error reports, whether written by yourself or imported third-party jar files (which often needs to be imported in J2EE ).

The above content from the http://www.java2000.net/p225,Java learning talk (a), I feel that it is easy to understand, take it to tidy up

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.