Java JNI programming skills in windows -- Java calls C/C ++ (1)

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.