Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze the working principle and characteristics of JVM

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

In this issue, the editor will bring you about how to analyze the working principle and characteristics of JVM. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

To give you a brief introduction to the working principle and characteristics of JVM, when we run and debug Java programs, we often mention the concept of JVM. JVM is the environment in which Java programs run, but it also runs an application and a process in an operating system, so it also has its own running life cycle and its own code and data space.

Detailed explanation of the working principle and characteristics of JVM

The working principle and characteristics of JVM mainly means that the operating system loads JVM through Java.exe in jdk, and completes the JVM environment through the following four steps.

1. Create a JVM mount environment and configuration

two。 Mount JVM.dll

3. Initialize JVM.dll and hang it to JNIENV (JNI calling API) instance

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

When we run and debug Java programs, we often mention the concept of a JVM. JVM is the environment in which Java programs run, but it is also an application and a process in an operating system, so it also has its own running life cycle and its own code and data space.

First of all, let's talk about jdk in how JVM works. Whether you are a beginner or an expert, a j2ee programmer or a j2se programmer, jdk is always doing something for us. Before we get to know Java, the masters will first give us something to say jdk. What role does it play in the entire Java system? I am amazed at the design genius of sun masters who can structure such a complete system. Jdk acts as a production and processing center in this system, producing all data output and being the execution center of all instructions and strategies.

It provides a complete scheme of Java and can develop all the applications and system programs that Java can support at present. If we talk about a question here, we will ask why there are such things as j2mejee. The purpose of these two things is very simple, and they are used to simplify the development and construction process in their respective fields. Apart from JVM, JDK also has some core components such as API, integrated API, user tools, development technology, development tools and API.

All right, after all the nonsense, let's get something related to the topic. JVM is in the * layer of the entire jdk, responsible for the interaction of the operating system, used to shield the operating system environment, and provides a complete Java environment, so it is a virtual computer. The operating system loads the JVM through the Java.exe in jdk, and the JVM environment is completed in the following four steps.

1. Create a JVM mount environment and configuration

two。 Mount JVM.dll

3. Initialize JVM.dll and hang it to JNIENV (JNI calling API) instance

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

Now that you know how JVM works, let's take a look at how to build a JVM environment.

One. JVM loads the environment. JVM provides the way to dynamically connect files to the operating system.

Since it is a file, it is a question of loading path, how does Java find this path? When you call Javatest, the operating system will be under path in your Java.exe program, Java.exe through the following process to determine the path of JVM and related parameters configuration. The following analysis based on Windows implementation.

First, look for the jre path. Java obtains the current Java.exe absolute path through GetApplicationHomeapi, c:\ j2sdk1.4.2_09\ bin\ Java.exe, then it will intercept the absolute path c:\ j2sdk1.4.2_09\ to determine whether the c:\ j2sdk1.4.2_09\ bin\ Java.dll file exists, and if so, use c:\ j2sdk1.4.2_09\ as the jre path If it does not exist, determine whether c:\ j2sdk1.4.2_09\ jre\ bin\ Java.dll exists, if it exists, c:\ j2sdk1.4.2_09\ jre as the jre path. If it does not exist, the path that calls GetPublicJREHomeCheck HKEY _ LOCAL_MACHINE\ Software\ JavaSoft\ JavaRuntimeEnvironment\ "current JRE version number"\ JavaHome is the jre path.

Then loading the JVM.cfg file JRE path +\ lib+\ ARCH (CPU framework) +\ JVM.cfgARCH (CPU framework) is judged by the GetArch function in Java_md.c, in which there are only two cases for the windows platform: WIN64's' ia64', 'other cases are' i386'. Take mine as an example: C:\ j2sdk1.4.2_09\ jre\ lib\ i386\ JVM.cfg. The main contents are as follows:

-clientKNOWN-serverKNOWN-hotspotALIASED_TO-client-classicWARN-nativeERROR-greenERROR

There are JVM.dll files in jre\ bin\ server and jre\ bin\ client in our jdk directory, and Java manages these different versions of JVM.dll through the JVM.cfg configuration file. Through the file, we can define which JVM is supported in the current jdk. The first part (client) is the JVM name, followed by the parameter, KNOWN indicates the existence of JVM, ALIASED_TO means to give another JVM an alias, WARN means to find a JVM replacement if it does not exist. ERROR indicates that there is no exception thrown. When running JavaXXX, Java.exe will check the current JVM type through CheckJVMType. Java can specify a specific JVM type through two parameters, one according to the JVM name in the JVM.cfg file, and the second method is directly specified. The methods they execute are "Java-J", "Java-XXaltJVM=" or "Java-J-XXaltJVM=" respectively.

If the parameter is passed by * *, the CheckJVMType function will take the JVM name after the parameter'- J', and then look among the known JVM configuration parameters. If the one with the same name is found, the'- 'before the JVM name is removed will directly return the value.

The second method directly returns the JVM type name after "- XXaltJVM=" or "- JmurXXaltJVM =". If you do not specify either of the above two methods when running Java, CheckJVMType will take the JVM name from the * configuration in the configuration file, remove the'- 'before the name and return this value. This return value of the CheckJVMType function is combined with the jre path to form the absolute path of JVM.dll in the following function. If not specified, this will use * defined JVM in JVM.cfg. It can be tested on the console through set_Java_LAUNCHER_DEBUG=1.

* get the path of JVM.dll. The JRE path +\ bin+\ JVM type string +\ JVM.dll is the file path of JVM. However, if you use the path path specified by the-XXaltJVM= parameter when calling the Java program, you will directly use the path+\ JVM.dll file as the file path of JVM.dll.

Two: load JVM.dll

The path to JVM has been found through the * step, and Java loads the JVM.dll file through LoadJavaVM. The loading task is simply to call the WindowsAPI function:

LoadLibrary loads the JVM.dll dynamic link library. Then attach the export functions JNI_CreateJavaVM and JNI_GetDefaultJavaVMInitArgs in JVM.dll to the CreateJavaVM and GetDefaultJavaVMInitArgs function pointer variables of the InvocationFunctions variable. The loading of JVM.dll has been completed.

Third, initialize the JVM to get the local call interface.

In this way, you can call the function of JVM in Java. Call InvocationFunctions- > CreateJavaVM, which is an example of the JNI_CreateJavaVM method in JVM to get the JNIEnv structure.

Four: run the Java program.

There are two ways for Java programs, one is jar package, the other is class. When running jar,Java-jarXXX.jar, Java.exe calls the GetMainClassName function, which first gets the JNIEnv instance and then calls the method getManifest () in the Java class Java.util.jar.JarFileJNIEnv and takes the value of getAttributes ("Main-Class") from the returned Manifest object, that is, the file in the jar package: the main class name of Main-Class specified by META-INF/MANIFEST.MF as the running main class. The main function then calls the LoadClass method in Java.c to load the main class (using the FindClass of the JNIEnv instance). The main function directly calls the LoadClass method in Java.c to load the class. If you are executing the class method. 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

The "publicstaticvoidmain (String [] args)" method, and determines whether the method is a public method, and then calls the

The CallStaticVoidMethod method calls the main method of the Java class.

The above is the editor for you to share how to analyze the working principle and characteristics of JVM, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report