JNI (Java Native Interface), jninative
I have done it on the Internet, but it is not successful. Due to work needs, we used JNI again and recorded the general process.JNI: Short for Java Native Interface (Java Local Interface. Using this interface, you can easily call the Dynamic Link Library (dll) file in java to implement some C/C ++ functions. Example: 1. Write a Java class and declare which functions are implemented by a dynamic library.
1 public class testdll {2 static {3 System. loadLibrary ("dllname"); // does not contain [. dll] 4} 5 6 public native void sayHello (); 7 8 public static void main (String [] args) {9 testdll test = new testdll (); 10 test. sayHello (); 11} 12}
2. Generate the header file testdll. h
The content of javah testdll. H is as follows:
1 /* DO NOT EDIT THIS FILE - it is machine generated */ 2 #include <jni.h> 3 /* Header for class testdll */ 4 5 #ifndef _Included_testdll 6 #define _Included_testdll 7 #ifdef __cplusplus 8 9 extern "C" {10 #endif11 /*12 * Class: testdll13 * Method: sayHello14 * Signature: ()I15 */16 17 JNIEXPORT void JNICALL Java_testdll_sayHello(JNIEnv *, jobject);18 #ifdef __cplusplus19 }20 #endif21 #endif
The function name has a specific format and cannot be modified at will. Java_class_method can only be called by class objects, because JNI searches for corresponding methods through class names.
3. Use VS to create an empty dll project and import the generated header file to the project. Create a new. c or. cpp file to implement the function in the header file:
1 #include <stdlib.h>2 3 JNIEXPORT void JNICALL Java_testdll_sayHello4 (JNIEnv *env, jobject obj){5 printf("hello world\n");6 }
4. compile and generate dll files
Set jni. h. Copy the jni_md.h file to the include directory of the VS environment. h ------------------ jdk installation directory \ include jni_md.h ------------- jdk installation directory \ include \ win32 compiled to generate testdll. dll 5. the execution will be compiled. place the dll file in the path environment variable directory, or in the eclipse project and directory, and then execute the Java program. You can also use the following parameters to specify the directory of the dll file java-Djava. library. path = dll file
Native definition in java
JNI is short for Java Native Interface. Since Java 1.1, the Java Native Interface (JNI) standard has become part of the java platform. It allows Java code to interact with code written in other languages. JNI was initially designed for locally compiled languages, especially C and C ++, but it does not prevent you from using other languages, as long as the call conventions are supported.
Using java to interact with locally compiled code usually results in loss of platform portability. However, in some cases, this is acceptable or even necessary. For example, you can use old libraries to interact with hardware and operating systems, or to improve program performance. The JNI standard should at least ensure that local code can work under any Java Virtual Machine implementation.
Writing steps of JNI (Java Native Interface)
· Compile a java class with native declarations
· Use the javac command to compile the compiled java class
· Use javah? Jni java class names generate header files with the extension h
· Use C/C ++ (or other programming languages) to implement local methods
· Generate a dynamic connection library for files written in C/C ++
1) Compile a java program:
The following uses HelloWorld as an example.
Code 1:
Class HelloWorld {
Public native void displayHelloWorld ();
Static {
System. loadLibrary ("hello ");
}
Public static void main (String [] args ){
New HelloWorld (). displayHelloWorld ();
}
}
Declare native METHOD: If you want to use a method as a local method, you must declare the method to native and cannot implement it. The parameters and return values of the method are described later.
Load dynamic library: System. loadLibrary ("hello"); load the dynamic library (we can understand this: our method displayHelloWorld () is not implemented, but we use it directly below, therefore, it must be initialized before use.) static blocks are usually used for loading. Note that the parameter "hello" of System. loadLibrary (); is the name of the dynamic library.
Main () method
2) There is nothing to say about compilation.
Javac HelloWorld. java
3) generate a header file with the extension of h
Javah? Jni HelloWorld
Header file content:
/* Do not edit this file-it is machine generated */
# Include
/* Header for class HelloWorld */
# Ifndef _ Included_HelloWorld
# Define _ Included_HelloWorld
# Ifdef _ cplusplus
Extern "C "{
# Endif
/*
* Class: HelloWorld
* Method: displayHelloWorld
* Signature: () V
*/
JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld
(JNIEnv *, jobject );
# Ifdef _ cplusplus
}
# Endif
# Endif
(In this case, we can understand the full text of ......>
Native usage in java
The java native method refers to a local method. It must be declared as a native method when calling code that is not written by the java language or directly manipulating computer hardware using the java language in the method.
In java, JNI (Java Native Interface, java Local Interface) is used to implement localization.
The Native method is generally used in two cases:
1) call some code in the method that is not written by the java language.
2) The method uses java to directly manipulate computer hardware.