Something more about classloader

Source: Internet
Author: User

As we all know, the parent-delegation mode is used in classloader after Java 2 to simplify the implementation of user-defined classloader and improve security.
Classloader is usually the first barrier for Java security,

    1. It will prevent malicious attacks.CodeInterference with the original trusted code.
    2. Protects trusted code.
    3. Classify different codes (called protection domains or runtime domain ).

the classloader assigns each loaded class to a unique namespace ([I] Name-space [/I]) at runtime. Different classes are assigned to different names-spaces based on the loaded class loader. JVM maintains a unique name-space name for each class loader. Once a Class A has been loaded and placed under name-space N, it is impossible to load another class A under the same name-space. If user-defined class loader is used, a linkageerror (duplicate class definition: [your class name]) is obtained. . Sometimes this name-space is also called [I] runtime package [/I]. In Java, the package access member (friendly) actually means that the runtime package access is visible, not during compilation. Therefore, when you try to access members that are not in the same runtime package, they are loaded by different class loaders even though they are in the same package during compilation) JAVA. lang. illegalaccessexception: Class A can not access a member of Class B with modifiers "" .

a user-defined must depend on other class loaders. At least system class loader is required to help it load some classes. After 1.2, each class loader except Bootstrap class loader has a parent class loader. Each time a class needs to be loaded, the system first requests the parent class loader to load the class. If the class fails, the system automatically loads the class. This is called the parent-delegation model.
If you violate this agreement, you will usually get a noclassdeffounderror . Because your class loader cannot find some classes in Java APIs, such as Java. Lang. object.
you may wonder if you can put the classes in Java APIs in the path of your class loader? JVM won't allow you to do this. You will get an exception like JAVA. Lang. securityexception: prohibited package name: Java. Lang, . After Java 1.2, all Java core APIs are loaded by a built-in class loader of JVM, And the Java security mechanism does not allow these classes or interfaces to be loaded by other class loaders.

In a default JVM environment, there are usually two class loaders. One is the so-called Bootstrap class loader, which is used to load all Java core APIs (including classes under all Java packages), and the other is the system class loader, it is used to load the class under your defined class_path.
The class. getclassloader () method is usually used to query the information of the Class Loader that loads this class. But for Sun's JVM implementation, for core API class, doing so only produces null. (See Java Doc ).

PS: The system class loader is Sun. Misc. launcher $ appclassloader.

Address: http://www.chinaitpower.com/A200507/2005-07-24/165821.html

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.