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

What are the working principles and characteristics of JVM

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article is to share with you about the working principle and characteristics of JVM, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

When we run and debug Java programs, we often mention the concept of JVM. Here we describe the working principle and characteristics of JVM. Java Virtual Machine (JVM) is a specification for computing devices that can be implemented in different ways (software or hardware).

Working principle and characteristics of JVM

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 of 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 JVM through the Java.exe in jdk, and the JVM environment is completed 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.

I. JVM mount environment

The way provided by JVM is the dynamic connection file of 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 finds 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 loads 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','is' 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 has JVM.dll files in our jdk directory, jre\ bin\ server and jre\ bin\ client, and Java manages these different versions of JVM.dll through the JVM.cfg configuration file. Through the file, we can define which JVM are supported in the current jdk. The first part (client) is the JVM name, followed by the parameter, and KNOWN indicates that JVM exists. ALIASED_TO means to give another JVM an alias, WARN means to find a JVM replacement if it does not exist, and ERROR means that there is no exception thrown.

◆ is running JavaXXX, Java.exe will check the current JVM type through CheckJVMType, Java can specify the specific JVM type through two parameters, one is specified according to the JVM name in the JVM.cfg file, the second method is to specify directly, the methods they perform 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 for the'- 'before removing the JVM name if the same name is found, and directly return the value; while the second method will directly return the JVM type name after "- XXaltJVM=" or "- JLV XXaltJVM=" If you do not specify either of the above two parameters 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 JVM and 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.

These are the working principles and characteristics of JVM. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please 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