Java JNI implementation principle (3) RegisterNatives method in JNI

Source: Internet
Author: User

We often use javah to generate JNI header files and then implement the JNI method defined by ourselves. This method is more traditional, we can see that the defined format and even the name must follow the standard [cpp] JNIEXPORT jint JNICALL Java_test_symlink (JNIEnv *, jobject, jstring, jstring); the complete structure is Java_classpath_classname_native method name, in this way, the corresponding native method can be found according to the naming rule when the jvm runs. In fact, jvm also provides the direct RegisterNative Method for manual registration of native methods. The following is a code example [cpp] static JNINativeMethod methods [] = {"retrieveDirectives ","() ljava/lang/assertionstatusdireves; ", (void *) & JVM_AssertionStatusDirectives}; (* env)-> RegisterNatives (env, cls, methods, sizeof (methods) /sizeof (JNINativeMethod); RegisterNative (JNIEnv, jclass cls, JNINativeMethod * methods, jint number) 1. methods is a two-dimensional array that represents the implementation method corresponding to each native method in this class. In the previous example, it indicates that a native method retrieveDiretives, and the returned value is AssertionStatusDirectives, the local method for execution is jvm_assertionstatusdireves ves 2. the following number indicates the number of native to be specified. The implementation of RegisterNative is very simple, the address of the native method in the class + 1 points to the function address of the executed c code, that is, the above & JVM_AssertionStatusDirectives [cpp] address * native_function_addr () const {assert (is_native (), "must be native"); return (address *) (this + 1 );

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.