Java supports C, C + + calls using collation through JNI technology

Source: Internet
Author: User
Tags naming convention

Recently in the company engaged in Terminal 5.0, said to the C + + version of the SDK package a Java version of the SDK, but the team did not engage in Java, so gig, temporary study for a few days, the whole a bit, feeling very interesting, so tidy up;

Why use Java to support calls to C, C + +?

It is well known that the Java language is a cross-platform interpretive language; #.java generates #.class bytecode by Javac compile generation, the running process relies on the Java virtual machine to execute bytecode to generate the virtual machine execution instructions, and finally through the virtual machine to control the platform to complete its design functions ; Its advantages are obvious, support cross-platform, its shortcomings are obvious, execution efficiency bottom, and does not support the platform personality function, and in the face of increasingly satisfying people's needs of embedded devices and mobile devices, due to their own limitations on the efficiency of the higher requirements, so many of the underlying functions are used in C + + to complete Therefore, Java to use this part of the function, must at the expense of its cross-platform advantage to provide support for other language calls to the access port, especially C, C + +, and finally provide the JNI technology support Java to other language calls;

In general, when there are some tasks that the Java language cannot handle, developers can use JNI techniques to do so. In general, the following scenarios require the use of JNI technology:
At development time, you need to invoke some of the features that the Java language does not support that depend on the features of the operating system platform. For example, if you need to invoke a feature of the current UNIX system, and Java does not support this feature, you need to use JNI technology to implement it.
Second, in the development, in order to integrate some of the previous non-Java language development of some systems. For example, you need to use some of the features or systems developed in the C or C + + language developed earlier to integrate these features into your current system or new version.
When developing, in order to save the running time of the program, some low-level or intermediate language must be adopted. For example, in order to create a time-saving application, it was necessary to use assembly language and then invoke the application of this low-level language through JNI technology in Java. According to the statistical JNI interface call than the direct use of interface calls, only 0.1 milliseconds delay, basically do not affect efficiency;

How do I use Java to support calls to C, C + +?     

Now take Test.java as an example, package name is Com.yueye, for example;

One, the method of defining the native interface in Test.java public static native void Testjni ();

Second, through Javac Test.java generation Test.class;

Third, through the Javah–o Test_jni.h–classpath. –jni com.yueye.test generates TEST_JNI.H, which contains a method:

Jniexport void Jnicall java_com_yueye_test__ (jnienv *, Jclass, jint);

The naming convention for this function is the Java_ package name _ function name __ parameter symbol

Iv. realizing java_com_yueye_test__ and generating dynamic library;

In Java, load or LoadLibrary into the dynamic library, and then call the native method directly;

Attach a complete example: Http://files.cnblogs.com/cainiaozhanchi/hello-world.zip

a summary of C, C + + call process problems using Java

First, the failure to guide the main library has the following reasons;

<1> libraries and dependent libraries are placed in the environment variable path;

<2> all libraries are complete;

<3> all libraries are valid;

Second, the use of JNI technology to import the library after successful call function to suspend the failure, mainly for several reasons;

The function name in <1> library is correct;

<2> function parameters are correct;

<3> whether the JNI function is implemented;

<4>jni.h function encoding is used in standard C-style, because the JNI technology calls C, C + +, only support C-style, if not the C-style function calling convention will be inconsistent, resulting in the corresponding function is not found, such as 3, function declaration in the A.H implementation is in A.cpp;

<5> the implementation of the function itself is problematic; this can be seen by stepping into it.

Finally, because of Java and JNI are just contact, there are sorting the wrong place, we point out, and then a follow-up on the use of JNI and the article on the principle of JNI technology understanding of the article;

Java supports C, C + + calls using collation through JNI technology

Related Article

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.