Java中JNI的使用详解第八篇:重载的实现

第一步: 编写java

public class HelloWorld{

    static {
        System.loadLibrary("native");
    }
    public native void callJavaFun(String str);
    public native void callJavaFun(String str,String param);

    public static void main(String[] args) {
        HelloWorld world=new HelloWorld();
        world.callJavaFun("say");
        world.callJavaFun("say","I am param");
    }

    public void say(){
        System.out.println("I AM CLASS");
    }

    public void say(String str){
        System.out.println(str);
    }
}

第二步: 编译、获取,更改JNI头文件名

$ javac HelloWorld.java
$ javah HelloWorld
$ mv HelloWorld.h  HelloWorld.c

第三步完成JNI文件的编写

#include 
JNIEXPORT void JNICALL Java_HelloWorld_callJavaFun__Ljava_lang_String_2
  (JNIEnv * env, jobject pramjob, jstring jstr){

const char *str;
    //
    str=(*env)->GetStringUTFChars(env,jstr,NULL);
    if (str==NULL)
    {
        return ;
    }

    //
    jmethodID mid  = NULL;

    jclass cls = (*env)->GetObjectClass(env,pramjob);

    mid = (*env)->GetMethodID(env,cls,str,"()V");

    (*env)->CallVoidMethod(env,pramjob,mid);

  }

JNIEXPORT void JNICALL Java_HelloWorld_callJavaFun__Ljava_lang_String_2Ljava_lang_String_2
  (JNIEnv * env, jobject pramjob, jstring jstr, jstring jstr2){

    const char *str;
    //
    str=(*env)->GetStringUTFChars(env,jstr,NULL);
    if (str==NULL)
    {
        return ;
    }

    const char *str2;
    //
    str2=(*env)->GetStringUTFChars(env,jstr2,NULL);
    if (str==NULL)
    {
        return ;
    }
    //
    jmethodID mid  = NULL;

    jclass cls = (*env)->GetObjectClass(env,pramjob);

    mid = (*env)->GetMethodID(env,cls,str,"(Ljava/lang/String;)V");

    (*env)->CallVoidMethod(env,pramjob,mid,jstr2);
  }

第四步: 编译、设置程序共享库位置

$ gcc HelloWorld.c -shared -fPIC -o libnative.so -I /usr/lib/jvm/java-7-openjdk-amd64/include/
$ export LD_LIBRARY_PATH=.

第五步: 执行、打印成功!

I AM CLASS
I am param

Jni提供的更多调用方法:
   jobject     (*CallObjectMethod)(JNIEnv*, jobject, jmethodID, ...);
    jobject     (*CallObjectMethodV)(JNIEnv*, jobject, jmethodID, va_list);
    jobject     (*CallObjectMethodA)(JNIEnv*, jobject, jmethodID, jvalue*);
    jboolean    (*CallBooleanMethod)(JNIEnv*, jobject, jmethodID, ...);
    jboolean    (*CallBooleanMethodV)(JNIEnv*, jobject, jmethodID, va_list);
    jboolean    (*CallBooleanMethodA)(JNIEnv*, jobject, jmethodID, jvalue*);
    jbyte       (*CallByteMethod)(JNIEnv*, jobject, jmethodID, ...);
    jbyte       (*CallByteMethodV)(JNIEnv*, jobject, jmethodID, va_list);
    jbyte       (*CallByteMethodA)(JNIEnv*, jobject, jmethodID, jvalue*);
    jchar       (*CallCharMethod)(JNIEnv*, jobject, jmethodID, ...);
    jchar       (*CallCharMethodV)(JNIEnv*, jobject, jmethodID, va_list);
    jchar       (*CallCharMethodA)(JNIEnv*, jobject, jmethodID, jvalue*);
    jshort      (*CallShortMethod)(JNIEnv*, jobject, jmethodID, ...);
    jshort      (*CallShortMethodV)(JNIEnv*, jobject, jmethodID, va_list);
    jshort      (*CallShortMethodA)(JNIEnv*, jobject, jmethodID, jvalue*);
    jint        (*CallIntMethod)(JNIEnv*, jobject, jmethodID, ...);
    jint        (*CallIntMethodV)(JNIEnv*, jobject, jmethodID, va_list);
    jint        (*CallIntMethodA)(JNIEnv*, jobject, jmethodID, jvalue*);
    jlong       (*CallLongMethod)(JNIEnv*, jobject, jmethodID, ...);
    jlong       (*CallLongMethodV)(JNIEnv*, jobject, jmethodID, va_list);
    jlong       (*CallLongMethodA)(JNIEnv*, jobject, jmethodID, jvalue*);
    jfloat      (*CallFloatMethod)(JNIEnv*, jobject, jmethodID, ...) __NDK_FPABI__;
    jfloat      (*CallFloatMethodV)(JNIEnv*, jobject, jmethodID, va_list) __NDK_FPABI__;
    jfloat      (*CallFloatMethodA)(JNIEnv*, jobject, jmethodID, jvalue*) __NDK_FPABI__;
    jdouble     (*CallDoubleMethod)(JNIEnv*, jobject, jmethodID, ...) __NDK_FPABI__;
    jdouble     (*CallDoubleMethodV)(JNIEnv*, jobject, jmethodID, va_list) __NDK_FPABI__;
    jdouble     (*CallDoubleMethodA)(JNIEnv*, jobject, jmethodID, jvalue*) __NDK_FPABI__;
    void        (*CallVoidMethod)(JNIEnv*, jobject, jmethodID, ...);
    void        (*CallVoidMethodV)(JNIEnv*, jobject, jmethodID, va_list);
    void        (*CallVoidMethodA)(JNIEnv*, jobject, jmethodID, jvalue*);

你可能感兴趣的:(Java中JNI的使用详解第八篇:重载的实现)