How the JVM Works

Source: Internet
Author: User

The principle and characteristics of the JVM is mainly that the operating system is loaded into the JVM through the JDK Java.exe to complete the JVM environment through the following 4 steps.

1. Creating a JVM load environment and configuration

2. Loading JVM.dll

3. Initialize JVM.dll and hang bounds to jnienv (JNI call interface) instance

4. Call the JNIEnv instance to mount and process the class.

When we run and debug Java programs, we often refer to the concept of a JVM. The JVM is the environment in which the Java program runs, but he also has a process for an application of an operating system, so he has his own running life cycle, as well as his own code and data space.

Let's start with the JDK in the JVM's working principle, whether you're a beginner or a master, a Java EE programmer or a J2SE programmer, The JDK is always doing something for us. Before we get to know Java, the Masters will provide us with the JDK. What role does it play in the entire Java system? I marvel at the design genius of Sun Masters, It's so perfect to have such a complete architecture. The JDK acts as a production and processing center in this system, producing all the data output, which is the execution center of all directives and strategies. itself it provides a complete solution to Java, You can develop all the applications and system programs that are currently supported by Java. Here's a question, and you'll ask, why are there j2me,j2ee these things, these two things are simple enough to simplify the development and build process in their respective fields. The JDK has some core APIs in addition to the JVM, Integration of APIs, user tools, development techniques, development tools and API components

Well, that's a lot to talk about, and something about the subject matter. The JVM is at the bottom of the entire JDK and is responsible for the interaction of the operating system, which masks the operating system environment, provides a complete Java runtime environment, and therefore the virtual machine. The operating system is loaded into the JVM through the JDK Java.exe to complete the JVM environment through the following 4 steps.

1. Creating a JVM load environment and configuration

2. Loading JVM.dll

3. Initialize JVM.dll and hang bounds to jnienv (JNI call interface) instance

4. Call the JNIEnv instance to mount and process the class.

A. The JVM loads the environment and the JVM provides the dynamic connection file for the operating system. since it's a file, it's just a matter of loading the path, how does Java find the path? When you call Java test, the operating system will be under path in your Java.exe program, Java.exe the following procedure to determine the path of the JVM and the associated parameter configuration. The following is an analysis of Windows-based implementations.

First find the JRE path, Java is through the Getapplicationhome API to get the current Java.exe absolute path, C:\j2sdk1.4.2_09\bin\Java.exe, then it will intercept the absolute path to c \ J2sdk1.4.2_09\, determine whether the C:\j2sdk1.4.2_09\bin\Java.dll file exists, if there is a c:\j2sdk1.4.2_09\ as the JRE path, if not exist then Judge C:\j2sdk1.4.2_09\ Whether the Jre\bin\java.dll exists if this c:\j2sdk1.4.2_09\jre exists as the JRE path. If there is no call getpublicjrehome Hkey_local_machine\software\javasoft\java Runtime environment\ "Current JRE version number" \ The path to the javahome is the JRE path.

Then mount the jvm.cfg file JRE path +\lib+\arch (CPU Architecture) +\jvm.cfgarch (CPU architecture) is judged by Java_ MD.C in the Getarch function, the function in the Windows platform only two cases: WIN64 ' ia64 ', the other case is ' i386 '. Take my example: C:\j2sdk1.4.2_09\jre\lib\i386\JVM.cfg. The main contents are as follows:

-client known-server known-hotspot aliased_to-client-classic warn-native error-green ERROR

There are JVM.dll files in Jre\bin\server and jre\bin\client in our JDK directory, and Java is managing these different versions of JVM.CFG through the JVM.dll configuration file. Through the file we can define the current JDK support those JVMs, the previous part (client) is the JVM name, followed by the parameters, known indicates that the JVM exists, aliased_to to the other JVM to take an alias, warn that does not exist when looking for a JVM substitution, The error indicates that no throw exception exists. In running Java XXX Yes, Java.exe will check the current JVM type through Checkjvmtype, Java can specify the specific JVM type by two parameters, one is specified by the JVM name in the Jvm.cfg file, the second method is directly specified, and the method they execute is " Java-j "," java-xxaltjvm= "or" java-j-xxaltjvm= ". If the first parameter is passed, the Checkjvmtype function takes the name of the JVM after the argument '-j ', and then looks for the '-' directly returning the value before removing the JVM name from the known JVM configuration parameter, and the second method returns directly to the-XXALTJVM The JVM type name after = "or"-j-xxaltjvm= ", if either of the above methods is not specified when running Java, Checkjvmtype takes the name of the JVM from the first configuration in the configuration file, removing the '-' from the front of the name to return the value. This return value of the Checkjvmtype function combines the JRE path into the absolute path of JVM.dll in the following function. If not specified, this will use the first defined JVM in Jvm.cfg. The set _java_launcher_debug=1 can be tested on the console.

Finally, the path to JVM.dll is obtained, and the JRE path +\BIN+\JVM type string +\jvm.dll is the file path of the JVM, but if the path is specified with the-xxaltjvm= parameter when calling the Java program, it is used directly path+\ JVM.dll file as a JVM.dll file path.

Two: Load JVM.dll

The path to the JVM has been found through the first step, and Java is loaded into the JVM.dll file by LOADJAVAVM. Loading a job is a simple call to a Windows API function:

LoadLibrary load JVM.dll dynamic Connection library. Then JNI_CREATEJAVAVM and jni_ the exported functions in the JVM.dll Getdefaultjavavminitargs is attached to the CREATEJAVAVM and Getdefaultjavavminitargs function pointer variables of the invocationfunctions variable. The loading work of the JVM.dll is declared complete.

Three: Initialize the JVM and get the local calling interface so that the JVM's functions can be called in Java. Call INVOCATIONFUNCTIONS->CREATEJAVAVM, the Jni_createjavavm method in the JVM, to get an instance of the JNIENV structure.

Four: Run the Java program.

Java programs have two ways: a jar package and a class. Run Jar,java-jar When Xxx.jar runs, Java.exe calls the Getmainclassname function, which first obtains the JNIEnv instance and then calls the Java class Java.util.jar.JarFileJNIEnv method Getmanifest () The value of GetAttributes ("Main-class") from the returned MANIFEST object is the file in the jar package: Meta-inf/manifest. The main class of the Main-class specified by MF is the main class of the run. The main function is then called JAVA.C in the LoadClass method to load the master class (Findclass using the jnienv instance). The main function directly calls JAVA.C in the LoadClass method to load the class. If you are executing the class method. The main function directly calls JAVA.C in the LoadClass method to load the class.

The main function then calls the Getstaticmethodid method of the JNIEnv instance to find the loaded class in the main class

public static void Main (string[] args) and determines whether the method is a public method, and then invokes the jnienv instance of the

The Callstaticvoidmethod method invokes the main method of the Java class.

How the JVM Works

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.