Menu Home

使用Invocation API调用Java类

InvocationApiTest.java

import java.io.*;

public class InvocationApiTest {
    public static void main(String[] args) {
        System.out.println(args[0]);
    }
}

invocationApi.c

#include <jni.h>

int main() {
    JNIEnv* env;
    JavaVM* vm;
    JavaVMInitArgs vm_args;
    JavaVMOption options[1];
    jint res;
    jclass cls;
    jmethodID mid;
    jstring jstr;
    jclass stringClass;
    jobjectArray args;

    //1.生成Java虚拟机选项
    options[0].optionString = "-Djava.class.path=.";
    vm_args.version = JNI_VERSION_1_6;
    vm_args.options = options;
    vm_args.nOptions = 1;
    vm_args.ignoreUnrecognized = JNI_TRUE;

    //2.生成Java虚拟机
    res = JNI_CreateJavaVM(&vm, (void**)&env, &vm_args);

    //3.查找并加载类
    cls = (*env)->FindClass(env, "invocationApiTest");

    //4.获取mian()方法的ID
    mid = (*env)->GetStaticMethodID(env, cls, "main", "([Ljava/lang/String;)V");

    //5.生成字符串对象,用作main()方法的参数
    jstr = (*env)->NewStringUTF(env, "Hello Invocation API");
    stringClass = (*env)->FindClass(env, "java/lang/String");
    args = (*env)->NewObjectArray(env, 1, stringClass, jstr);

    //6.调用main()方法的
    (*env)->CallStaticVoidMethod(env, cls, mid, args);

    //7.销毁Java虚拟机
    (*vm)->DestroyJavaVM(vm);

    return 0;
}

MAC上的编译参数:

gcc -Wall -g -o invocationApi invocationApi.c -I$JAVA_HOME/include  -I$JAVA_HOME/include/darwin  -L$JAVA_HOME/jre/lib/server -ljvm -rpath $JAVA_HOME/jre/lib/server

ref: jdk-7u45-apidocs/technotes/guides/jni/spec/invocation.html#wp9502
ref: 《Android框架揭秘》

Categories: android framework C/C++ java macos

Tagged as:

lnmcc