Basic usage of Java JNI

Source: Internet
Author: User

JNI is an interface for Java to interact with C, C ++, objective-C, objective-C ++, other static compilation languages, and assembly languages. Although Java currently provides many runtime libraries with high runtime performance, Java does not provide many efficient libraries in many aspects, especially in the high-performance computing field, therefore, we can use the JNI interface to connect the static language and Assembly compilation to a dynamic library and load and call the Java application.

First, Java provides a suitable runtime environment for different operating system platforms and JNI header files based on different compilers. JNI header files are generally composed of two types: jni_md.h provides platform-dependent header files; JNI. H provides interface declarations required by JNI and various types of definitions. Both header files can be found in JDK include. When creating a JNI dynamic library project, we should set the project's output target to a dynamic Connection Library. DLL in the Unix-like system. so, OS X is. dylib ). When creating a project, we can import these two header files to the project.

Then we can look at the following code:

#include <stdio.h>#include "jni.h"JNIEXPORT jint JNICALL Java_MyJNI_myTest(JNIEnv *env, jobject obj, jintArray dstArray, jintArray srcArray){    jboolean isCopy = 0;    jint* csrcArray = (*(*env)->GetIntArrayElements)(env, srcArray, &isCopy);    jsize dstSize = (*(*env)->GetArrayLength)(env, dstArray);    jsize srcSize = (*(*env)->GetArrayLength)(env, srcArray);    jsize length = dstSize >= srcSize? dstSize : srcSize;        printf("The length is: %u\n", length);    printf("Is copy available? %d\n", isCopy);    printf("The sum of source array is: %d", csrcArray[0] + csrcArray[1]);        jint dstBuffer[32];        for(jsize i = 0; i < length; i++)        dstBuffer[i] = csrcArray[i] + i + 100;        (*(*env)->SetIntArrayRegion)(env, dstArray, 0, length, dstBuffer);        return 100;}

The code above provides an export Java method. The name after Java _ indicates the class name at the Java application end, the name of the _ following the class name is the method name of the class (this method can be a member method or a class method ). A standard JNI method should provide two parameters: one is jnienv * and the other is jobject. Env provides a handle to the Java Runtime Environment. this parameter is required for calling various Java runtime Methods later. Jobject is a pointer to this object, which is equivalent to this in the Java application. Therefore, if the JNI method you define is a class method on the Java application end, this parameter is ignored.

The above method adds 100 to each element of the second array and the value after the index is assigned to the first target array element. Assume that the number of elements in the two arrays is 32 at most. The last return value is 100 of the int type.

When using GCC in Linux, you must manually add-FPIC in the C language compilation option; otherwise, the connection will fail.

Then, we can look at the corresponding Java code:

class MyJNI {    native static int myTest(int[] dstArray, int[] srcArray);}public class Test {    static {        System.loadLibrary("MyJNI");    }    /**     * @param args     */    public static void main(String[] args) {        // TODO Auto-generated method stub        int[] dstArray = new int[2];        int[] srcArray = {100, 600};        System.out.println("The value is: " + MyJNI.myTest(dstArray, srcArray));        System.out.println("The sum is: " + (dstArray[0] + dstArray[1]));    }}

Call the system. loadlibrary method to load the dynamic Connection Library.

When using the Linux environment, you must set the LD_LIBRARY_PATH environment variable before running to specify the path of the dynamic library, for example:

LD_LIBRARY_PATH = '/usr/home/java_test'

Export LD_LIBRARY_PATH

If you use the eclipse development environment, you can set this environment variable in the run configuration and debug configuration of the current project. For convenience, we can use the variable button on the right when setting the path and select project_loc. This built-in variable specifies the absolute System Path of the current project. Then, import the dynamic library to the root directory of the current project.

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.