JVM class loading mechanism (ClassLoader) source Analysis (3)

Source: Internet
Author: User

Java ClassLoader, there is also a special function is to load the local library. This feature is related to the keyword native. Simply put, the local library that calls c++/c (Windows is suffix. dll,linux is suffix. So).

The call is to use the system class, where two methods are as follows:
Load (String filename)
LoadLibrary (String libname)
One is based on the filename, one according to the Lib name. Note that the file name is not the same as the Lib name, otherwise the error is written and the load failure information is reported.
The system class is a very special class that interacts with the bottom layer and sees a lot of native keywords that will be described in detail when parsing system and runtime.

Back to the previous topic, both methods were eventually invoked to Classloader.loadlibrary (Fromclass, Name,isabsolute). The method is shown in figure:


Description of the following parameters:
Fromclass This parameter is the class instance of the current caller, for example:
public class test{
public void Load () {
System.loadlibrary ("1232");
}
}
Then the Fromclass parameter is class<test>.
The Isabsolute parameter is simple, whether it is a rule path (for example: ...). /.. /test.java).
The name parameter may be a filename or a libname way.

The first stage of this method is based on the Downloadmanager judgment, the static method DownloadFile of the Downloadmanager class is invoked if the JRE is not completed and is currently being downloaded by a thread.
The Downloadmanager class, which belongs to the Sun.jkernel package, is related to the JRE component, simply installing the JRE's storage path.
Sometimes the JRE is embedded in the position of Java_home/jre, sometimes in a separate location, especially when installed under Windows operating systems, when you are prompted to install the JRE and you can choose to store the path at any time.
This class is called when the JVM is initialized, and Downloadmanager This class also supports command-line operations. More details will be introduced in the future.

Because of the complexity, a lot of calls to the local method, so I just briefly introduced here.
The Java.library.path is a Java system variable whose value equals the path in the system variable.
Sun.boot.library.path is a Java system variable whose value is equal to Java_home/jre/bin.

The main is to load the local library with 4 processes.
The 1th step is to determine whether the rule path, true, immediately after the conversion of the rule path to load, if the load failed to throw the exception directly and perform step 5, and then perform step 2.
The rule path is to use the. Or. Such symbols.
2nd Step if the class loader is not empty, get libname information from class loading,
The abstract class ClassLoader, however, is always returned null (see the Classloader.findlibrary method), so it basically executes step 3 directly (unless it is a custom class loader that overrides this method).
The 3rd step is to load the search libname from all the paths in the Sun.boot.library.path first, if step 5 is successfully performed, and step 4 is performed.
The 4th step is to first determine whether the class loader is empty, not empty, will search libname from all paths in Java.library.path to load, regardless of success, will perform step 5.
The 5th step is completed.

In addition, the local library name (already loaded) has three stored properties in the code, as follows:
All native library names we ' ve loaded.
private static vector loadedlibrarynames = new vector ();
Native libraries belonging to system classes.
private static vector systemnativelibraries = new vector ();
Native libraries associated with the class loader.
Private vector nativelibraries = new vector ();


Storing relevant information after loading is saved using the Nativelibrary class, the loading mechanism is closely related to JNI technology, and using JNI will have a better understanding of the load function.

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.