Java calls the C ++ program through JNI

Source: Internet
Author: User

JNI is the abbreviation of Java Native Interface. Using JNI can easily use our Java program to call C/C ++ programs. Many times, some functions cannot be implemented using Java. For example, if some underlying driver functions are involved, we can use JNI to call C or C ++ programs, this is the strength of JNI. However, JNI also has its disadvantages. Using java to interact with locally compiled code usually results in a loss of platform portability.

The following is a JNI example that calls C ++ to output "hello world ":

 

Step 1: Create a Java class and define a local method in it (the method modified with the native keyword)

Public native void sayHello ();

Step 2: Use the javah command (full path of the javah class) to generate the C ++ header file of the local method

In the DOS window, enter the project directory and run javah com. test. after the TestNative command is executed, a suffix named. h header file, such as com_test_TestNative.h. This header file is named based on the package name and class name.

  #include <jni.h>     _Included_com_test_TestNative           JNIEXPORT    (JNIEnv *     

Lines 15 and 16 are declarations of the local method sayHello () in the TestNative class. This H file is equivalent to our interface in java. Here we declare a Java_com_test_TestNative_sayHello (JNIEnv *, jobject); method, and then implement this method in our local method, that is to say, the method name we use when writing a C/C ++ program must be consistent with the method name here.

Step 3: write local C/C ++ code to generate dynamic link library files

First, create a dll project-Win32 Dynamic-Link Library Project in VC6.0 (of course other tools can be used. Add the header file com_test_TestNative.h generated above to the project, create a source file to reference the header file, and implement the function of the local function in the header file:

     #include<iostream.h>     #include      JNIEXPORT  JNICALL Java_com_test_TestNative_sayHello(JNIEnv *          cout<<<<     }

 

Jni. h is introduced in com_test_TestNative.h, so we need to add jni. h to the Include directory under the installation directory of VC6.0. Jni. h is included in the JDK installation directory, and two headers jawt_md.h and jni_md.h in the include/win32 file must also be imported to VC6.0.

After importing the dependent header file, we can build the project. Press F7 and a dynamic link library file will be generated under the Degug directory in the project directory, nativeCode is generated here. dll. We can copy the dll file to the Directory included in the environment variable path and call it to our Java program. For convenience, we can also add the project directory where the dll is located to the environment variable path to avoid the trouble of copying it every time. Note that you must restart myeclipse after modifying environment variables.

 

Step 4: Call Local functions in Java

                                            System.loadLibrary("NativeCode"         TestNative tNative =    }

Row 10th is used to load the dynamic link library. The JVM can call the Dynamic Link Library only once. "NativeCode" is the name of the dynamic link library generated above, excluding the suffix name.

Run the program and print "hello world ".

 

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.