Joke Collection Website - Public benefit messages - The problem of using JNI when C calls java! urgent. . . . .

The problem of using JNI when C calls java! urgent. . . . .

M is one of its components, see below for more details:

--------------------- -------------

The operating system loads jvm through java.exe in jdk. The jvm environment is completed through the following 4 steps.

1. Create jvm loading environment and configuration

2. Load jvm.dll

3. Initialize jvm.dll and mount it to JNIENV (JNI call interface) 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 jvm. jvm is the environment in which java programs run, but it is also an application and process of an operating system, so it also has It has its own running life cycle and its own code and data space.

First of all, let’s talk about jdk. Whether you are a beginner or an expert, a j2ee programmer or a j2se programmer, jdk always It is helping us do something. Before we understand java, the masters will first provide us with the jdk. What role does it play in the entire java system? I am amazed that the design geniuses of the sun masters can make such a The complete system is so perfectly structured. JDK acts as a production and processing center in this system, produces all data output, and is the execution center of all instructions and strategies. It itself provides a complete solution for Java, which can be developed and supported by Java currently. All applications and system programs. Let me talk about a question here. Everyone will ask, why are there such things as j2me and j2ee? The purpose of these two things is very simple. They are used to simplify the development and construction process in their respective fields. In addition to jvm, jdk In addition, there are also some core APIs, integrated APIs, user tools, development technologies, development tools and APIs.

Okay, so much nonsense, let’s talk about the topic. JVM is at the bottom of the entire JDK and is responsible for the interaction of the operating system. It is used to shield the operating system environment and provide a complete Java operating environment, so it is also a virtual computer. The operating system loads the JVM through java.exe in the JDK. Complete, complete the jvm environment through the following 4 steps.

1. Create jvm loading environment and configuration

2. Load jvm.dll

3. Initialize jvm .dll and link it to the JNIENV (JNI calling interface) instance

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

1. jvm loading environment, the method provided by jvm is the dynamic connection file of the operating system. Since it is a file, it is a matter of loading path. How does java find this path? When you call java test, the operating system will add your java.exe program under path. java.exe will determine the path of the jvm and related parameter configuration through the following process. The following analysis based on windows implementation.

First find the jre path. Java obtains the current java.exe absolute path through GetApplicationHome api, c:\j2sdk1.4.2_09\bin\java.exe, then it will intercept the absolute path c: \j2sdk1.4.2_09\, determine whether the c:\j2sdk1.4.2_09\bin\java.dll file exists. If it exists, 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 is used as the jre path. If it does not exist, call GetPublicJREHome to check the path of HKEY_LOCAL_MACHINE\Software\JavaSoft\Java Runtime Environment\"Current JRE version number"\JavaHome, which is the jre path.

Then load the jvm.cfg file JRE path \lib \ARCH (CPU architecture) \jvm.cfgARCH (CPU architecture). The judgment is through the GetArch function in java_md.c. In this function, the windows platform has only Two cases: 'ia64' for WIN64, 'i386' for all other cases. Take mine as an 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

In our Both jre\bin\server and jre\bin\client have jvm.dll files in the 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 currently supported in jdk. The first part (client) is the name of the jvm, followed by the parameters. KNOWN means that the jvm exists, ALIASED_TO means to give another jvm an alias, WARN means to find a jvm replacement if it does not exist. ERROR means no exception was thrown. When running java Methods are specified directly, and the methods they execute are "java -J", "java -XXaltjvm=" or "java -J-XXaltjvm=".

If it is the first parameter passing method, the CheckJvmType function will take the jvm name after the parameter '-J', and then search it from the known jvm configuration parameters. If it finds the same name, remove the '-' before the jvm name and return it directly. value; and the second method will directly return the jvm type name after "-XXaltjvm=" or "-J-XXaltjvm="; if neither parameter of the above two methods is specified when running java, CheckJvmType It will take the jvm name in the first configuration in the configuration file, remove the '-' in front of the name and return the value. The return value of the CheckJvmType function will be combined with the jre path in the following function to form the absolute path of jvm.dll. If not specified, this will use the first jvm defined in jvm.cfg. You can test it on the console by setting _JAVA_LAUNCHER_DEBUG=1.

Finally get the path of jvm.dll. JRE path\bin\jvm type string\jvm.dll is the file path of jvm, but if you use the path specified by the -XXaltjvm= parameter when calling the java program path, just use the path \jvm.dll file directly as the file path of jvm.dll.

Two: Load jvm.dll

The path to jvm has been found through the first step, and java loads the jvm.dll file through LoadJavaVM. The loading work is very simple, just call the windows API function:

LoadLibrary loads the jvm.dll dynamic link library. Then hook the exported 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 is declared complete.

Three: Initialize the jvm and obtain the local calling interface, so that you can call jvm functions in java. Call InvocationFunctions-gt;CreateJavaVM, which is the JNI_CreateJavaVM method in jvm to obtain an instance of the JNIEnv structure.

Four: Run the java program.

There are two ways of java program, one is jar package and the other is class. When running jar, java -jar XXX.jar is running, java.exe calls the GetMainClassName function, which first obtains the JNIEnv instance Then call the method getManifest() in the java class java.util.jar.JarFileJNIEnv and get the value of getAttributes("Main-Class") from the returned Manifest object, which is the file in the jar package: Main specified by META-INF/MANIFEST.MF The main class name of -Class is used as the main class to run. The main function will then call 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 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 "public static void main(String[] args)" method in the loaded class main class, and determines whether the method is public method, and then call the

CallStaticVoidMethod method of the JNIEnv instance to call the main method of the java class.

In addition, the virtual machine group purchase of products is super cheap