JNI _7_c Call Java method One

Source: Internet
Author: User

1. Declaring methods in Java

<span style= "FONT-SIZE:14PX;" >/** * javah-encoding utf-8-jni com.example.jniandroid.service.CFunction * When there is Chinese in Java, a call to encode the GBK non-mapped character method is reported. Plus-encoding utf-8  *  * @author Libin *  */public class Cfunction {/** * implementation in C callback callback method */public native void Calljninative ();/** * C calls the instance method of Java */public void callback () {System.out.println ("callnative to callback in  Java");} /** * C calls the static method of Java */public static void Callbackstatic () {System.out.println ("callnative to  callback static in  Java ");}} </span>

2. Call Java method in C2.1 Invoking instance methods

The implementation of the local method Java_instancemethodcall_nativemethod demonstrates two steps in calling a Java method in local code:

1, the local method first calls the JNI function Getmethodid. This function looks for the appropriate method in the specified class. The search process is based on the method descriptor. If the method does not exist, GETMETHODID returns NULL. At this point, it immediately returns from the local method and throws a nosuchmethoderror error.

2. The local method calls an instance method with a return value of void by calling Callvoidmethod.

In addition to the Callvoidmethod function, JNI also supports calls to methods that return values to other types. If you call a method that returns a value of type int, your local method uses Callintmethod. Similarly, you can call Callobjectmethod to invoke a method that returns a value of object type such as Java.lang.String, array, and so on.

2.2 Calling a static method

One of the JNI series functions of the static method invocation function, such as: Callstaticvoidmethod,callstaticbooleanmethod, etc.

There is a big difference between calling a static method and a JNI function that invokes an instance method, which is a class reference, which is a reference to an object instance.

accessing a static method in Java can be done through a class, or through an object instance. The JNI rule is that when you callback a static method in Java in your local code, you must specify a class reference.

<span style= "FONT-SIZE:14PX;" >/** * Call Java static methods and instance methods */jniexport void Jnicall java_com_example_jniandroid_service_cfunction_calljninative ( JNIENV * env, Jobject obj) {//Call static method Jclass CLS = (*env)->getobjectclass (env, obj); Jmethodid callbackstatic = (*env)-> ; Getstaticmethodid (env, CLS, "Callbackstatic", "() V");(*env)->callstaticvoidmethod (env, CLS, callbackstatic);// To invoke an instance method, first create an object and then call it through the Call<type>method method jclass Instancefunc = (*env)->findclass (env, "com/example/ Jniandroid/service/cfunction ") Jmethodid construction_id = (*env)->getmethodid (env, Instancefunc," <init> " , "() V"); Jclass mcfunction = (*env)->newobject (env, Instancefunc, construction_id); Jmethodid callback  = (*env)- >getmethodid (Env,instancefunc, "Callback", "() V");(*env)->callvoidmethod (env, Mcfunction, callback);} </span>


JNI _7_c Call Java method One

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.