Step 2: compile Java code
Next, we need to compile the Java code into bytecode.
One way to do this is to use the Java compiler javac provided with the SDK.
The command used to compile Java code into bytecode is:
CD Test
Javac jni_javacallc_test.java
If the above Code is written in the eclipse environment, the above Java file is automatically generated under the bin of the project directory when the file is saved.
Step 3: Create a C/C ++ header file
The third step is to create the C/C ++ header file, which defines the local function description.
One way to complete this step is to use javah.exe, which is a local method C stub Generator tool provided along with the SDK.
This tool is designed to create header files that define C-style functions for each native method found in the Java source code file.
The command used here is:
CD Test
Javah-classpath. Test. jni_javacallc_test
Note: there is a space between. and test.
The following files are generated:
/* DO NOT EDIT THIS FILE - it is machine generated */#include <jni.h>/* Header for class test_JNI_javaCallc_test */#ifndef _Included_test_JNI_javaCallc_test#define _Included_test_JNI_javaCallc_test#ifdef __cplusplusextern "C" {#endif/* * Class: test_JNI_javaCallc_test * Method: intMethod * Signature: (I)I */JNIEXPORT jint JNICALL Java_test_JNI_1javaCallc_1test_intMethod (JNIEnv *, jobject, jint);/* * Class: test_JNI_javaCallc_test * Method: booleanMethod * Signature: (Z)Z */JNIEXPORT jboolean JNICALL Java_test_JNI_1javaCallc_1test_booleanMethod (JNIEnv *, jobject, jboolean);/* * Class: test_JNI_javaCallc_test * Method: stringMethod * Signature: (Ljava/lang/String;)Ljava/lang/String; */JNIEXPORT jstring JNICALL Java_test_JNI_1javaCallc_1test_stringMethod (JNIEnv *, jobject, jstring);/* * Class: test_JNI_javaCallc_test * Method: intArrayMethod * Signature: ([I)I */JNIEXPORT jint JNICALL Java_test_JNI_1javaCallc_1test_intArrayMethod (JNIEnv *, jobject, jintArray);#ifdef __cplusplus}#endif#endif
C/C ++ header files
As you may have noticed, the C/C ++ function in jni_javacallc_test.h differs greatly from the Java Native method declaration in jni_javacallc_test.java.
1. jniexport and jnicall are the pointers used to export functions dependent on the compiler.
2. The return type and parameter type are C/C ++ types mapped to the Java type, such as jstring and jint.
Here we will introduce the data types in JNI:
In C ++, the compiler allocates the length for some basic data types based on the platform where the compiler is located, which leads to platform inconsistency. This problem does not exist in Java, because of JVM, the basic data types in Java get the same length on all platforms. For example, the width of Int Is always 32 bits. For this reason, some mapping needs to be implemented for the basic data types of Java and C ++ to maintain consistency.
The following table can be summarized: The subscript lists common ing tables from C/C ++ to Java.
Java type |
Local type |
Alias defined in JNI |
Int |
Long |
Jint |
Long |
_ Int64 |
Jlong |
Byte |
Signed Char |
Jbyte |
Boolean |
Unsigned char |
Jboolean |
Char |
Unsigned short |
Jchar |
Short |
Short |
Jshort |
Float |
Float |
Jfloat |
Double |
Double |
Jdouble |
Object |
_ Jobject * |
Jobject
|
The JNI designer has helped us get the corresponding alias for convenience of memory. For more detailed information, see the header file JNI. h. The definitions and aliases of various data types are defined in this file.
Except for common parameters declared in Java, all the parameter tables of these functions have a pointer to jnienv and jobject.
The pointer to jnienv is actually a pointer to the function pointer table.
As we will see in Step 4, these functions provide various capabilities to operate Java data in C and C ++.
The jobject parameter references the current object.
Therefore, if C or C ++ code needs to reference a Java function, jobject acts as a reference or pointer and returns the called Java object.
The function name is composed of the prefix "Java _" and fully qualified class names, followed by underscores and method names.