Mutual calls between C/C ++ and Java

Source: Internet
Author: User

Using JNI technology, we can call each other between C/C ++ and Java.

 

1. Java calls the C/C ++ Interface

This is the most basic application of JNI. JNI has many sample codes for this purpose. C/C ++ is generally written as a dynamic link library. In the Java program loadlibrary, the call can be completed using the Native Interface.

Note: In Mac, the name of the dynamic link library is not necessarily libhello. So. It may be libhello. jnilib.

 

2. C/C ++ callback Java Interface

In this usage, there are few examples of JNI. In fact, it is relatively simple, and JNI also provides such an interface. For example, getobjectclass, getmethodid, callobjectmethod, newobject, findclass, and other interfaces can call C/C ++ on Java interfaces.

In addition, one thing to note is about signature. For example, in jmethodid getmethodid (jclass clazz, const char * Name,

The third parameter in the const char * sig function requires the signature of the function. How can we know the signature of the Java method?

In fact, JDK provides the signature for viewing Java methods. The method is as follows:

(1) cmd: Go to the directory where XXX. Class is located.

(2) enter the command javap-s xxx

(3) The results are displayed in front of you.

 

 

Extension: Android ndk is similar. It can also achieve interaction between C/C ++ and Java.

 

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.