More detailed introduction to JNI

In Java, sometimes we have to use code in other languages, such as:

1. Your application needs to access various features and devices of the system, which cannot be accessed through the Java platform.

2. You already have a lot of tested and debugged code written in another language and know how to export it to all target platforms.

3. Through basic tests, you have found that the Java code written is much slower than the equivalent code written in other languages.

The Java platform has an API for interoperation with local C code, called Java local interface (JNI). Now start learning JNI. First, learn how to call local methods using JNI from the simplest example in this article.

Call the local method and print out the step example of HelloWorld

1. Declare a local method in a class

We use the native keyword to declare a local method, and we also declare it as static. Local methods can be static or not declared static.

2. Compile Java file to generate the bytecode of the class containing the local method

If you are in eclipse, go to < project Directory > \ bin \ com \ Tao \ test, and you can find the project that eclipse compiles automatically for us Class file.

If not, enter nativetest The directory where the java file is located, and use the javac command to compile the @ R_ 403_ 1713 @, generated nativetest Class bytecode.

3. Compile using the javah command Class bytecode to generate C / C + + header files containing native method declarations

If it is in eclipse, enter the < project Directory > \ bin directory, and then use the following compilation command (if it is not eclipse, compile it with javap according to the location of the. Class file compiled above)

1.javah com. tao. test. Nativetest / / remember to use the fully qualified name of the type

The C \ C + + header file com we need will be generated in the bin directory_ tao_ test_ NativeTest. H (the name of the header file contains the package name and is separated by), and the contents are as follows:

4. Write the C / C + + source file according to the generated C / C + + header file and compile it into DLL Dynamic link library

We got the C / C + + header file containing the native method declaration before. Now we write our C \ C + + source file according to this header file and compile it into DLL Dynamic link library.

Last step, we got com_ tao_ test_ NativeTest. H header file.

① First. Create a WIN32 DLL project (I use VS2005 development environment, and VC6.0 + + creates a WIN32 dynamic link library)

New ― > project ― > Visual C + + ― > Win32 ― > console application ― > enter the project name, which I use as nativecall

Click OK to enter the following interface

Click next to enter the next interface and check DLL and empty project.

This creates a WIN32 DLL project.

② , add com_ tao_ test_ NativeTest. H header file

Will com_ tao_ test_ NativeTest. Copy the H header file and paste it into the nativecall / nativecall / directory, such as e: \ VCProject \ nativecall \ nativecall. Then add the header file to the project in VS2005

③ , add JNI H and

Open com_ tao_ test_ NativeTest. H header file, look at the second line, called JNI h. This header file needs to be added by ourselves for Java_ In the home / include directory, the addition method is the same as step ②

Then put the #include < JNI in the second line above h> Change to #include "JNI. H". Let it call our own header file instead of the system header file.

Open JNI h. We can find such a line

1.#include "jni_md.h"

This header file needs to be added by ourselves. It is located in Java_ In the home / include / Win32 directory, the addition method is the same as before.

④ Write source files.

Create a new source file named nativemethod

Then, add the following code:

Finally, save, run and compile the generated DLL file, which can be found in the debug directory of the project.

5. Put the DLL file into the path environment variable.

Copy the path where the DLL file is located and add it to the path environment variable.

6. Call our local method in Java.

be careful:

1. Dynamic link libraries are usually loaded in static code blocks

2. If eclipse is on when the DLL path is added to the path environment variable, close eclipse and open it again to let eclipse read the environment variable again

3. You must use the native method in this class

The following figure shows the process of writing JNI

summary

The above is a detailed JNI profile introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

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
分享
二维码
< <上一篇
下一篇>>