Explain the use of Java local interface JNI in detail

Explain the use of Java local interface JNI in detail

For Java programmers, I don't think I have to say the benefits and advantages of the Java language. Naturally, you will say a lot. But although we are Java programmers, we have to admit that the Java language also has some shortcomings of its own. For example, it has its disadvantages in performance and dealing with the bottom layer. So Java provides some local interfaces. Its main function is to provide a standard way for Java programs to interact with native code through virtual machines, This is what we often call the Java Native Interface (JNI), which enables Java code running inside the Java virtual machine (VM) to communicate with other programming languages (such as C, C + + and assembly language) to interoperate with applications and libraries. The most important advantage of JNI is that it does not impose any restrictions on the implementation of the underlying Java virtual machine. Therefore, Java virtual machine manufacturers can add support for JNI without affecting other parts of the virtual machine. Programmers only need to write a version of local applications or libraries to communicate with the required There are Java virtual machines supporting JNI to work together. Let's take a look at why we interact with native code:

1: Improve application performance. We know that Java is "advanced" for C / C + + and assembly language. In fact, the advanced here simplifies the work of programmers. Many of the underlying things are done by the Java virtual machine. But after all, compared with direct access to the underlying layer, Java has one more step of the process of virtual machine, so its performance is a little slower than these native languages.

2: Realize some functions related to the bottom layer. The Java platform provides standard class libraries and powerful APIs, although it can complete most functions. However, some functions dealing with the underlying hardware cannot be completed in the class library provided by the Java API.

3: Integrate with existing programs written in native code. JNI can be used when the software written in C or C + + and other native languages is integrated into 0 on the operating system.

JNI interface functions and pointers

Platform related code accesses Java virtual machine functions by calling JNI functions. JNI functions can be obtained through interface pointers. An interface pointer is a pointer to a pointer, which points to a pointer array, and each element in the pointer array points to an interface function. Each interface function is in a predetermined offset of the array. The following figure illustrates the organization of interface pointers.

The organization of JNI interface is similar to C + + virtual function table or COM interface. The advantage of using interface tables instead of hard coded function tables is to separate the JNI namespace from platform related code. Virtual machines can easily provide multiple versions of JNI function tables. For example, a virtual machine can support the following two JNI function tables:

・ a table to comprehensively check the illegal parameters, which is applicable to the debugging program;

・ the other table only carries out the minimum inspection required by JNI specification, so it is more efficient.

The JNI interface pointer is only valid in the current thread. Therefore, local methods cannot pass interface pointers from one thread to another. The virtual machine implementing JNI can allocate and store the data of the local thread in the area pointed to by the JNI interface pointer.

Local methods accept JNI interface pointers as parameters. When the virtual machine makes multiple calls to the local method from the same java thread, it ensures that the interface pointer passed to the local method is the same. However, a local method can be called by different Java threads, so it can accept different JNI interface pointers.

(1) Write java class code

Among them, the methods that need to be implemented by JNI should be declared with the native keyword. In this class, use the system. 1oadlibrary () method to load the required dynamic link library. The key codes are as follows:

(2) Compile to bytecode

In this process, due to the use of native keyword declaration, the java compiler will ignore the JNI method without code body.

(3) Generate header files for related JNI methods

The implementation of this process is generally generated by using jlavah JNI * class (- JNI can be omitted), or the file can be generated manually; however, since the Java virtual machine calls the JNI method according to a certain naming specification, it needs special care to manually write the header file.

The code of the header file generated by the above file is as follows:

JNI function name is divided into three parts: the first is java keyword, which is recognized by Java virtual machine; Then the caller class name (fully qualified class name, where the underscore replaces the name separator); Finally, there is the corresponding method name, which is separated by underscores.

The parameters of JNI function are also composed of three parts: the first is jnienv *, which is a pointer to JNI running environment; The second parameter varies depending on whether the local method is static or non static - the second parameter of the non static local method is a reference to the object, while the second parameter of the static local method is a reference to its Java class; The remaining parameters correspond to the parameters of common Java methods, and the parameter types need to be mapped according to certain rules.

(4) Write the implementation code of the corresponding method

In the coding process, you should pay attention to the length of variables. For example, the length of integer variables in Java is 32 bits, while that in C language is 16 bits. Therefore, you should carefully check the variable type mapping table to prevent problems in the process of value transfer.

(5) Compile JNI implementation code into dynamic link library

The compilation process is realized by C / C + + compiler. On Windows platform, the result of compilation and connection is dynamic link library DLL file. When you want to use the generated dynamic link library, you need to explicitly call the link library DLL file in the caller class.

After the above processing, the development of a Java class containing localized methods is basically completed.

Appendix: mapping Jav types to local C types

For ease of use, the following definitions are provided.

The jsize integer type is used to describe the main index and size:

Troubleshooting

When using JNI to access native code from Java programs, you encounter many problems. The three most common errors you will encounter are:

1) Cannot find dynamic link. The error message it generates is: Java lang.UnsatisfiedLinkError。 This usually means that the shared library cannot be found, or a specific native method within the shared library cannot be found.

2) The shared library file could not be found. When using system When loading a library file with the loadlibrary (string libname) method (the parameter is the file name), ensure that the file name is spelled correctly and that no extension is specified. Also, ensure that the location of the library file is in the classpath, so as to ensure that the JVM can access the library file.

3) Cannot find method with specified description. Make sure that your C / C + + function implementation has the same description as the function description in the header file.

Conclusion

Calling C or C + + native code from Java (although not simple) is a well integrated function in the Java platform. Although JNI supports C and C + +, the C + + interface is clearer and generally preferable to the C interface. As you have seen, calling C or C + + native code requires giving a function a special name and creating a shared library file. When using an existing code library, it is usually not advisable to change the code. To avoid this, in C + +, proxy code or proxy classes are usually created, which have special named functions required by JNI. Then, these functions can call the underlying library functions, and the description and implementation of these library functions remain unchanged.

If you have any questions, please leave a message or go to the community of this site for exchange and discussion. Thank you for reading. I hope it can help you. Thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>