Deep understanding of java:1.1. Class Loader

Source: Internet
Author: User

From the Java dynamics to the class loading mechanism

We know that Java is a dynamic language.

So how do you understand this "dynamic"?

Or what is the nature of a language that can be called a dynamic language?

For Java, that's how I understand it.

We all know that the JVM (Java Virtual machine) is not executing local machine code instructions,

Instead, it executes an instruction called bytecode, which exists in the class file.

This requires the virtual machine to load the relevant class file into memory before actually executing the bytecode.

The virtual machine does not load all the required class files at once, because it does not know what class files will be used in the future when it executes.

It is a class file associated with this class that is loaded "dynamically" at run time, with each of the classes used.

This is the root cause of what Java is called a dynamic language.

In addition to dynamically loading classes, classes are dynamically initialized and dynamically linked.

The class loader (ClassLoader), which is responsible for loading classes in the JVM, is the class loader that this article describes, so the ClassLoader is an essential component of the JVM.

How ClassLoader and Class loaders work in Java

In Java (referred to as javase) there are three kinds of loaders.

Each class loader has specified their corresponding directories at the time of creation, that is, where each class loader goes to load the class is OK, I think there should be Gettargetpath () in the ClassLoader class, get their corresponding path, find the JDK's document, The discovery is not.

Here are the three kinds of loaders and their corresponding paths:

* Appclassloader--load the class in the path specified by classpath

* Extclassloader-Class under the directory that loads the Jre/lib/ext directory or java.ext.dirs the system attribute definition

* BootStrap--load classes in Jre/lib/rt.jar

So how does the ClassLoader work?

You can see the source code of the ClassLoader class in the JDK. The implementation of this class uses the template method pattern, first of all the LoadClass method to load the class, and the LoadClass method calls the Findclass method, which reads and returns the data of the class file, after the Findclass method returns, The LoadClass method continues to invoke the DefineClass method, which processes the returned data into the type information that is recognized by the virtual runtime.

So, if we develop our own classloader, we just need to inherit the ClassLoader class from the JDK, and overwrite the Findclass method, and the rest of the work, the parent class will be done.

  1. Class Myclassloader extends ClassLoader {
  2. @Override
  3. Public class<?> loadclass (String name) throws ClassNotFoundException {
  4. String fileName = name.substring (Name.lastindexof (".") + 1) + ". Class";
  5. InputStream in = GetClass (). getResourceAsStream (FileName);
  6. if (in = = null) {
  7. return Super.loadclass (name);
  8. }
  9. byte[] B = null;
  10. try {
  11. b = new byte[in.available ()];
  12. In.read (b);
  13. In.close ();
  14. } catch (IOException e) {
  15. E.printstacktrace ();
  16. }
  17. return DefineClass (name, B, 0, b.length);
  18. }
  19. }

Other Java platforms have implemented their own specific classloader, depending on their needs,

For example, the Tomcat server in the Java EE platform, the Dalvik virtual machine in the Android platform also defines its own classloader.

There are two ways to load a class in a virtual machine, one way is the Classloader.loadclass () method mentioned above,

The other is the use of the Reflection Api,class.forname () method, in fact the Class.forName () method is also used inside the ClassLoader.

Specifies that the argument classloader is specified; otherwise, the caller's ClassLoader is returned.

The implementation of the Forname method in class classes is as follows:

[Java]
  1. Public static class<?> forname (String name, boolean initialize,
  2. ClassLoader loader)
  3. throws ClassNotFoundException
  4. {
  5. if (loader = = null) {
  6. SecurityManager sm = System.getsecuritymanager ();
  7. if (sm! = null) {
  8. ClassLoader CCL = Classloader.getcallerclassloader ();
  9. if (CCL! = null) {
  10. Sm.checkpermission (
  11. Securityconstants.get_classloader_permission);
  12. }
  13. }
  14. }
  15. return FORNAME0 (name, initialize, loader);
  16. }
  17. /** called after security checks has been made. * *
  18. private static native Class FORNAME0 (String name, boolean initialize,
  19. ClassLoader loader)
  20. throws ClassNotFoundException;

Deep understanding of java:1.1. Class Loader

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.