Reprinted article as follows: http://www.cnblogs.com/jiayi/archive/2010/06/08/1753863.html
The jvmjob principle and special point are that the operating system is installed with jvmthrough jdk's java.exe, and the following four steps are used to complete the JVM environment.
1. Create a JVM loading environment and Configuration
2. Load JVM. dll
3. initialize JVM. dll and mount it to the jnienv (JNI call interface) instance.
4. Call the jnienv instance to load and process the class.
A jvm concept is often mentioned when we run and debug Java programs. JVM is the environment where Java programs run, but it is an application and a process of the operating system at the same time. Therefore, it also has its own running life cycle, it also has its own code and data space.
First, let's talk about the JDK In the JVM working principle. Whether you are a beginner or a master, a J2EE programmer or a j2se programmer, JDK is always doing something for us. before learning about Java, the masters will provide us with JDK. what role does it play in the entire Java System? I'm amazed by the design talents of masters sun, who can make such a complete system so perfectly structured. in this system, JDK acts as a production and processing center to generate all data outputs. It is the execution center of all commands and strategies. it provides a complete Java solution and can develop all the applications and system programs currently supported by Java. here is a question. You may ask, why are there other such things as j2-j2ee? These two things are designed to simplify the development and construction processes in their respective fields. in addition to JVM, JDK also includes core APIs, integrated APIs, user tools, development technologies, development tools, and APIs.
Well, I have talked a lot about theme-related things. JVM is at the bottom layer of the entire JDK and is responsible for operating system interaction. It is used to shield the operating system environment and provide a complete Java running environment. Therefore, it is also a virtual computer. the operating system is installed with jvmthrough java.exe in JDK. The following four steps are used to complete the JVM environment.
1. Create a JVM loading environment and Configuration
2. Load JVM. dll
3. initialize JVM. dll and mount it to the jnienv (JNI call interface) instance.
4. Call the jnienv instance to load and process the class.
1. JVM is loaded into the environment. The JVM provides dynamic connection files of the operating system.Since it is a file, it is a mount path problem. How does Java find this path? When you call Java test', the operating system will be in your java.exeprogram under path. java.exe uses the following process to determine the JVM path and related parameter configurations. The following is an analysis of the implementation based on windows.
First, find the JRE path. Java uses getapplicationhome apito upload the absolute path of Java. c: \ j2sdk1.4.2 _ 09 \ bin \ java.exe. Then it intercepts the absolute path c: \ j2sdk1.4.2 _ 09 \, judge c: \ j2sdk1.4.2 _ 09 \ bin \ Java. whether the DLL file exists. If yes, use c: \ j2sdk1.4.2 _ 09 \ as the JRE path. If no, use c: \ j2sdk1.4.2 _ 09 \ JRE \ bin \ Java. whether the dll exists. If the c: \ j2sdk1.4.2 _ 09 \ JRE exists, use it as the JRE path. if you do not call getpublicjrehome To Check HKEY_LOCAL_MACHINE \ SOFTWARE \ javasoft \ Java
Runtime Environment \ "Current JRE version number" \ javahome path is JRE path.
Then load the JVM. cfg file JRE path + \ Lib + \ arch (CPU architecture) + \ JVM. CFG arch (CPU architecture) is determined by the getarch function in java_md.c. In this function, there are only two windows platforms: 'ia64' in win64 and 'i386' in other cases '. Take my example: C: \ j2sdk1.4.2 _ 09 \ JRE \ Lib \ i386 \ JVM. cfg. The main content is as follows:
- -client KNOWN
- -server KNOWN
- -hotspot ALIASED_TO -client
- -classic WARN
- -native ERROR
- -green ERROR
In our JDK directory, JRE \ bin \ Server and JRE \ bin \ Client both have JVM. the DLL file exists, while Java uses JVM. CFG configuration file to manage these different versions of JVM. DLL. through the file, we can define the JVM supported by JDK. The previous part (client) is the JVM name, followed by the parameter. Known indicates that the JVM exists, and aliased_to indicates that an alias is given to other JVMs, warn indicates finding a JVM replacement when there is no such exception, and error indicates that no exception is thrown. when running Java xxx, java.exe checks the current JVM type through checkjvmtype. Java can specify the specific JVM type through two parameters, one according to JVM. the JVM name in the cfg file is specified. The second method is to directly specify the JVM name.
-J "," Java-xxaltjvm = ", or" Java-J-xxaltjvm = ". For the first parameter transfer method, the checkjvmtype function takes the JVM name after the '-J' parameter, then, find from the known JVM configuration parameters. If the same name is found, remove the '-' before the JVM name to directly return the value. The second method, the JVM type name after "-xxaltjvm =" or "-J-xxaltjvm =" is directly returned. If no one of the above two methods is specified when running Java, checkjvmtype takes the JVM name in the first configuration in the configuration file, and removes '-' before the name to return this value. The Return Value of the checkjvmtype function is combined with the JRE path in the following function to form the absolute path of JVM. dll. If this parameter is not specified, the first defined JVM in JVM. cfg is used.
_ Java_launcher_debug = 1 test on the console.
Finally, obtain the JVM. dll path, JRE path + \ bin + \ JVM type string + \ JVM. DLL is the JVM file path, but if you use the path specified by the-xxaltjvm = parameter when calling the Java program, you can directly use path + \ JVM. DLL file as JVM. DLL file path.
Ii. Load JVM. dll
The JVM path has been found through the first step. Java uses loadjavavm to mount the JVM. dll file. The installation is easy to call the Windows API function:
Loadlibrary loads the JVM. dll dynamic connection library. Then, the export functions jni_createjavavm and jni_getdefajavavminitargs in JVM. dll are mounted to the createjavavm and getdefajavjavavminitargs function pointer variables of the invocationfunctions variable. The installation of JVM. dll is completed.
3. initialize JVM and obtain the local call interface,In this way, you can call JVM functions in Java. Call invocationfunctions-> createjavavm, that is, the jni_createjavavm method in JVM to obtain the jnienv structure instance.
4. Run the Java program.
Java programs can be either a jar package or a class program. when running jar and Java-jar xxx.jarruntime, java.exe calls the getmainclassname function. This function obtains the jnienv instance and then calls the Java class Java. util. jar. the method getmanifest () in jarfilejnienv and get the value of getattributes ("Main-class") from the returned manifest object is the file in the jar package: META-INF/manifest. the main class name specified by MF is used as the main class for running. The main function then calls the loadclass method in Java. C to load the main class (using findclass of the jnienv instance ). The main function directly calls the loadclass method in Java. C to load the class. If the class method is executed. The main function directly calls the loadclass method in Java. C to load the class.
Then the main function calls the getstaticmethodid method of the jnienv instance to find the loaded class main class.
"Public static void main (string [] ARGs)" method, determine whether the method is a public method, and then call the jnienv instance's
The callstaticvoidmethod method calls the main method of the Java class.