Step 1: Write Java code
Starting from writing a Java source code file, it will declare the local method (or method), load the shared library containing the local code, and then actually call the local method.
Here is jni_javacallc_test:
Directly use a text editor or create a project in ecilpos and type the following code:
Package test; public class jni_javacallc_test {// C/C ++ local method public native int intmethod (int n); Public native Boolean booleanmethod (Boolean bool ); public native string stringmethod (string text); Public native int intarraymethod (INT [] intarray); // Java main method public static void main (string [] ARGs) {// contains the C language dynamic library system. loadlibrary ("test_jni_javacallc_test"); jni_javacallc_test sample = new jni_javacallc_test (); int square = sample. intmethod (5); Boolean bool = sample. booleanmethod (true); string text = sample. stringmethod ("Java"); int sum = sample. intarraymethod (New int [] {1, 1, 2, 3, 5, 8, 13}); system. out. println ("intmethod:" + square); system. out. println ("booleanmethod:" + bool); system. out. println ("stringmethod:" + text); system. out. println ("intarraymethod:" + sum );}}
What does this code do?
First, pay attention to the use of the native keyword, which can only be used with methods.The native keyword tells the Java compiler that the method is implemented using the native code outside the Java class, but its declaration is in Java. The native method can only be declared in the Java class, but cannot be implemented (but cannot be declared as an abstract method. Use the native keyword). Therefore, the native method cannot have the method subject.
Now let's look at the code line by line:
From row 6th to row 12th, we declare four native methods.
In row 3, we mounted shared library files that contain the implementations of these local methods. (By Step 5, we will create the shared library file .)
Finally, we called the local method from row 21st to row 27th.
Note: This operation is no different from the operation that calls non-local Java methods.