Although you can write applications entirely in Java, there are situations where Java itself does not meet the needs of your application. When an application cannot be written entirely in Java, programmers use JNI to write Java native methods to handle these situations. The following examples illustrate when you would need to use Java native methods: - The standard Java class library does not support platform-dependent features that your application requires.
- You already have a library written in another language and want to make it accessible to Java code through JNI.
- You want to implement a small portion of time-critical code in a lower-level language such as assembly language.
Source:https://docs.oracle.com/javase/1 ... ec/intro.html#wp725 Understand JNI functions JNI functions are local functions defined at the native layer, corresponding to methods declared using the native keyword at the Java layer. To put it bluntly, it is declared at the Java layer and implemented in C/C++ language. Of course, this function is not ordinary. It will be associated with the method of the Java layer through some mechanism of JNI, so that the Java layer code can call it very conveniently. JNI function syntax and calling rules - Convert java language data types into underlying language calling rules
- Convert java language method calls into underlying language functions or methods
JNI data type conversion jni establishes a connection between java and C, so jni must first unify the data types of the two
Java Type | Native Type | Description |
boolean | jboolean | unsigned 8 bits |
byte | jbyte | signed 8 bits |
char | jchar | unsigned 16 bits [/t d][/tr] |
short | jshort | signed 16 bits |
int | jint [/td][ td]signed 32 bits |
long | jlong | signed 64 bits |
float | jfloat | 32 bits [/td ][/tr] |
double | jdouble | 64 bits |
void | void | N/A |
The following definition is provided for convenience.
#define JNI_FALSE 0 #define JNI_TRUE 1 The jsize integer type is used to describe cardinal indices and sizes:
[font ="]typedef jint jsize; Java can directly call the functions of the underlying language. JNI specifies the method signatures of Java calling the underlying language. There is a concept of overloading in Java, so Java method conversion Into a signature, the signature is then converted into a functionType Signature | Java Type Z boolean ] |
B | byte |
C | char |
[align= left]S | short |
I | int |
J [/td][ td]long |
F | float |
D | double [/td][ /tr] |
L fully-qualified-class ; | fully-qualified-class |
[ type | type[] |
( arg-types ) ret -type | method type |
[size=3 ]For example, the Java method:
[font ="]long f (int n, String s, int[] arr); has the following type signature:
[font ="](ILjava/lang/String;[I)J jni mechanism implementation[ attach]402991[/attach]
Native declared functions may not be implemented class Cls { native double f(int i, String s); //Declare as a local method static { [/ font] System.loadLibrary("library name"); // Load the dynamic library through the static initialization statement block } } Program to implement test java code
javah -jni Hello Generate Hello .h
Open Hello .h The name of the C function has been written in the file. C code
Make the C file into a shared library .so. Pay attention to specify the jni.h and jni_md.h file paths. gcc -shared -fPIC hello.c -o libhello.so -I /usr/java/jdk1.6.0_45/include/ -I /usr/java/jdk1.6.0_45/include/linux/ java Hello Run ProgramExperimental Phenomenon