[Java class loader] Analysis of ClassLoader in Java.

Source: Internet
Author: User

This article is in the company internal TD written a small article, mainly on the Java ClassLoader Basic knowledge, now bring here to share.

First, the question

Please create the following class in Eclipse and run it:

1 package java.lang;2 3 public class Long {4 public     static void main (string[] args) {5         System.out.prin TLN ("Hi, I am here"    }7}   

Can you guess it's running if? Yes, that's what it looks like!

Error: The main method could not be found in class Java.lang.Long, define the Main method as:
public static void Main (string[] args)
Otherwise the JavaFX application class must be extended javafx.application.Application





Why, obviously I defined the main method in the Long method class, why is the main method not defined?

This article will address the reasons for these problems.

Second, the role of ClassLoader

We all know that after the Java program is written, A. Java (text file) file exists on the disk, and then we compile the. java file into a. class file (bytecode file) by using the (bin/javac.exe) compile command, and it exists on the disk. But to run the program, you must first load the. class file into the JVM's memory for use, and the ClassLoader is responsible for loading the. class file on the disk into the JVM memory, as shown in:

You can assume that each class object has the. Class bytecode content on the disk, and each class object has a getClassLoader () method to get the one who loaded me from the. class file into memory into a class object.

Third, ClassLoader hierarchy structure

Please perform the following procedure:

1 public class Test {2 public     static void main (string[] args) {3         ClassLoader ClassLoader = test.class
     
              System.out.println (ClassLoader); 5          6         classLoader classLoader1 =
       System.out.println (classLoader1) ; 8 9 ClassLoader classLoader2 = }12}   
      

Its output is:

[Email protected]
[Email protected]
Null

Get the CLASSLOADER2 is the null value. There are actually three class loaders here:

(1): root class loader (null)

It is implemented by the native code (c + +), and you cannot get his references at all, but he actually exists and loads some important classes that load (%java_home%\jre\lib), such as Rt.jar (runtime), I18n.jar and so on, these are the core classes of Java.

(2): Extension class loader (Extclassloader)

Although we can get it, but we seldom use it in practice, it mainly loads the jar package in the extended directory,%java_home%\lib\ext

(3): Application class loader (Appclassloader)

It mainly loads the classes in our application, such as test, or third-party packages, such as the JDBC driver package.

The parent class loader here is distinguished from the concept of inheritance in classes, which do not have a parent-child relationship on the class definition.

Iv. the order in which class loaders are called when class loads

When a class is to be loaded, there is a concept of starting the ClassLoader and the actual ClassLoader, see the following analysis

As the above test.class to be loaded, it will start the application ClassLoader to load the test class, but the application ClassLoader will not really load him, but will call to see if there is a parent loader, the result is an extension class loader, the extension class loader will not go directly to load, it is to see if they have a parent loader no, the result it still has is the root class loader.

So this time the root class loader is going to load this class, but under%java_home%\jre\lib, it can't find com.wangmeng.Test this class, so he tells his sub-class loader, I can't find, you go to load it, subclass extension ClassLoader go%java_home %\lib\ext to find, also can't find, it tells its sub-class loader appclassloader, I can not find this class, you go to load it, the result Appclassloader found, add to memory, and generate class object.
This time the class loader (application ClassLoader) and the actual class loader (application ClassLoader) are the same.

This is the famous delegation loading mechanism in Java , see:

Let's take a look at the load of Java.lang.Long, as analyzed above, should be loaded by the root ClassLoader, at this point the launcher is the application class loader, but the actual classloader is the root class loader.

So back to our very first question, there is no main method because the execution is not the class we write ourselves, the execution is the Java core of the long class, of course, there is no main method. This prevents classes written in our application from overwriting the Java core class.

[Java class loader] Analysis of ClassLoader in Java.

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.