JNI Notes 1, jni notes
1. What is JNI?
Java Native Interface (JNI) is a local programming Interface in Java.
It is a function interface for Java and the local operating system code to call each other.
2. Steps for Java to call C/C ++:
1. Declare the native method in the Java class
Create a Java project TestNativeCode package com. fomagic class Testnative
1 package com. fomagic; 2 3 public class TestNative {4 public native void sayHello (); // The local code of C ++ implements 5 public static void main (String [] args) {6 7} 8}2. Use the javah command to generate the C/C ++ header file containing the native method definition
3. Write the C/C ++ source file according to the generated C/C ++ header file.
1) create a Win32 console application in VS-> OK-> next (select application settings)
2) copy the three files to the project directory.
Header file generated in step 3 of com_fomagic_TestNative.h
Jni. h is located in the include file directory under JDK.
Jni_md.h include-> win32 Directory
3) Add the existing item com_fomagic_TestNative.h to introduce the header file ()
4) Add the following content to the source file source. cpp:
5) Note that the referenced header file "<>" indicates that the system and "indicate that local queries are preferred.
1 # include "inline" 2 # include <iostream>; 3 using namespace std; 4 5 // define method declared in com_fomagic_TestNative.h 6 JNIEXPORT void JNICALL inline (JNIEnv * evn, jobject obj) 7 {8 cout <"Hello World! "<Endl; 9}4. Compile the C/C ++ source file into a dynamic link library (DLL) and execute binary code.
Run the build solution to generate the DLL file in Debug under the project directory.
5. Add the PATH of the DLL file to the PATH environment variable.
This configuration of Java environment variables should be clear, so that Java programs can directly call the current dynamic link library
Note: You need to restart Eclipse to re-load environment variables to develop Java programs.
6. Load the DLL in the java class, and then call the declared native method.
1 package com. fomagic; 2 public class TestNative {3 public native void sayHello (); // C ++ local code implementation 4 public static void main (String [] args) {5 System. loadLibrary ("NativeCode"); // load the dynamic link library, which cannot be added. dll 6 TestNative test = new TestNative (); 7 test. sayHello (); 8} 9}7. Run the Java Project
1) execution result: Hello World!
2) exception information: Can't load IA 32-bit. dll on a AMD 64-bit platform
Solution:
When the Java referenced DLL Dynamic Link Library mentioned above, the execution result exception information is: Can't load IA 32-bit. dll on a AMD 64-bit platform
Cause: Because your windows operating system is 64-bit, the win32 console program generates a 32-bit DLL dynamic link library by default.
Solution: we can imagine that we need a 64-bit DLL dynamic link library to generate
1. Configure through the original VS Project ()
2. At this time, NativeCode. dll is generated in the project directory --> x64 --> Debug directory.
3. Similarly, add the PATH of the DLL file to the PATH environment variable to facilitate Java program loading. Eclipse needs to restart and reload the environment variable.
Iii. Disadvantages and points of attention