Jni
Before starting this article, let's first talk about JNI. Java is good, people use a lot, the application is very wide, but Java is not perfect. Java's shortcomings are much slower than traditional C + +, and Java has no direct access to the underlying operating system, such as a hardware system, and for this reason Java provides JNI to enable access to the underlying. Jni,java Native Interface, which is part of Java's SDK, JNI allows Java code to use code and code libraries written in other languages, and functions in local programs can invoke the Java layer function, where JNI implements two-way interaction between Java and native code.
Native
JDK open to the user's source code is ubiquitous native method, by the Native keyword declaration method shows that the method is not implemented in the Java language, but in the local language implementation, Java can be directly used. Here is a concept, the local language, the local language of the four words, personal understanding should be able to directly interact with the operating system language.
Calling C + + code written by JNI
Here's how to write a simple C + + code that uses a native method in Java to invoke the procedure. The IDE that C + + code uses is Microsoft Visual Studio 2010, which is the most common IDE for developing C + + code on the market and is powerful. OK, here's a step-by-step demonstration:
1. Write a section of Java code. Because we are in the Windows environment, so a. dll file is used, if it is in a Linux environment, a. So file is used. After the final C + + code is written to generate a. dll/.so file, the resulting file can be loaded using static statically loaded methods, or the former can be configured by configuring the environment variable.
1PublicClassTestmain2{3Static4{5 System.load ("D:" + file.separator + "Hello.dll"); } 7 8 public native static void Hello (); 9 10 public static void< Span style= "color: #000000;" > main (string[] args) 11 12 Hello () }14}
2, cmd into the command-line program, into the class just written under the Classpath, classpath is the root path where the. class file resides. Run "jni-jni com.xrq.test1.TestMain", which indicates that the. h file is generated for the native method under the specified class. The. h file is the header file used by C/s + + to separate the Declaration and implementation, and the classmates who are unfamiliar
3. Enter your own classpath to see if there is more than one. h file on my own side.
This name is Javah the implementation of this command to help us name, as long as this file can be
4. Open VS2010, file-to-new project, named "Hello", and the name of load in our static block to be consistent
Note Select DLL
5. Copy the. h file generated by Javah to the Hello directory just now
6. Add this. h to the project, right-click the header file---Add Existing Item, select "Com_xrq_test1_testmain.h" in the Hello directory to add it.
7. Right-click Source file--Add New item, select. cpp file, name it, I'm called Source.cpp. The implementation is simple, just print the string "Hello". Cout is a C + + console output statement, equivalent to Java System.out.print (), Endl is the meaning of a newline, so this whole sentence is equivalent to Java System.out.println ("Hello");
8, modify the "Com_xrq_test1_testmain.h" header file of # include <jni.h> to include "jni.h"
9, the %java_home%/include directory under the "Jni.h" and %java_home%/include/win32 directory under the "Jni_md.h" Two Files to the Hello directory, add these two. h files to the header file directory in the same way you just added "Com_xrq_test1_testmain.h", so there should be two more files in the Hello directory under the directory. There should be 3. h files in the VS2010 project directory. Java_home is the JDK installation directory, do not know the cmd into the command line,Echo%java_home% can be viewed. This step is important, without this step, C + + programs cannot be run
10. A. dll file can be generated by right-click Project--Build.
11. The. dll file that you just generated is a 32 system, and 32 users of the system can use the. dll file directly, just copy it to the directory specified in the static code block. In other words, the user of a 64-bit system cannot use this. dll file. If you want to build a. dll file that can be used by a 64-bit computer, you also have to create a. dll file for the 64-bit computer.
The drop-down menu does not have a x64-click Drop-down menu--Configuration Manager configuration, I feel good, VS2010 have their own x64
12. Generate a 64-bit. dll and copy it to the D drive, which is the path of the Hello.dll specified in the static code block.
13, run the first step inside the Java program written, C + + printing "Hello" came out, at this point, Java through the JNI call C + + written code to complete the function.
What's your experience?
I have completed such a process, it must be quite a sense of accomplishment, after the sense of accomplishment, we can from these 13 steps to comprehend what? At least individuals have the following experiences:
There are many native methods in the 1, 1 classes, and all the native methods in this class are generated into 1. h Files----local code generates a. dll/.so file and a class that corresponds to the native method implementation
2, why there is a native method of the class must have such a piece of code
void (); Static { registernatives (); 4}
Now, it is estimated that, as with our static code blocks, it is used to import specific. dll/.so files for this class. As for why can't we write like this, personal guess is because the. dll/.so file location on different user disks is not fixed, and is related to the JDK installation directory?
3, Java does not care about the native method is implemented in what language, as long as a language can and the bottom of the deal is good, and language implementation can provide. dll/.so file. So the same native method, if different Java virtual machines to call it, then the results may be different, such as Object Hashcode (), of course, the efficiency is not the same, because different virtual machines for different native methods have their own implementation.
Implement a call to the native method yourself