JNI——C调用JAVA
步骤:
1. 创建虚拟机
2. 获得class
3. 实例化对象:获得构造方法(方法名为“<init>”),构造参数,调用方法
4. 调用方法:又分为获得方法,构造方法,调用方法
操作方法:
1. 编译:javac Hello.java
2. javap -p -s Hello.class:查看Signature
3. gcc -I/usr/lib/jvm/java-1.8.0-openjdk-amd64/include/ -I/usr/lib/jvm/java-1.8.0-openjdk-amd64/include/linux/ -o caller caller.c -L /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/server -ljvm
4. 执行:LD_LIBRARY_PATH=/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/server ./caller
举例:
(1)call_static_method
1 #include <stdio.h> 2 #include <jni.h> 3 4 5 JNIEnv* create_vm(JavaVM** jvm, JNIEnv** env) 6 { 7 JavaVMInitArgs args; 8 JavaVMOption options[1]; 9 args.version = JNI_VERSION_1_6; 10 args.nOptions = 1; 11 options[0].optionString = "-Djava.class.path=./"; 12 args.options = options; 13 args.ignoreUnrecognized = JNI_FALSE; 14 return JNI_CreateJavaVM(jvm, (void **)env, &args); 15 } 16 17 int main(int argc, char **argv) 18 { 19 JavaVM* jvm; 20 JNIEnv* env; 21 22 jclass cls; 23 int ret = 0; 24 25 jmethodID mid; 26 27 /* 1. create java virtual machine */ 28 if(create_vm(&jvm, &env)) 29 { 30 printf("can not create jvm\n"); 31 return -1; 32 } 33 34 /* 2. get class */ 35 cls = (*env)->FindClass(env, "Hello"); 36 if(cls == NULL) 37 { 38 printf("can not find hello class\n"); 39 ret = -1; 40 goto destory; 41 } 42 43 /* 3. create object */ 44 45 /* 4. call method 46 * 4.1 get method 47 * 4.2 create parameter 48 * 4.3 call method 49 */ 50 51 mid = (*env)->GetStaticMethodID(env, cls, "main", "([Ljava/lang/String;)V"); 52 if(mid == NULL) 53 { 54 ret = -1; 55 printf("can not get method\n"); 56 goto destory; 57 } 58 59 (*env)->CallStaticVoidMethod(env, cls, mid, NULL); 60 61 destory: 62 (*jvm)->DestroyJavaVM(jvm); 63 64 return ret; 65 }
(2)call_non_static_method
1 #include <stdio.h> 2 #include <jni.h> 3 4 5 JNIEnv* create_vm(JavaVM** jvm, JNIEnv** env) 6 { 7 JavaVMInitArgs args; 8 JavaVMOption options[1]; 9 args.version = JNI_VERSION_1_6; 10 args.nOptions = 1; 11 options[0].optionString = "-Djava.class.path=./"; 12 args.options = options; 13 args.ignoreUnrecognized = JNI_FALSE; 14 return JNI_CreateJavaVM(jvm, (void **)env, &args); 15 } 16 17 int main(int argc, char **argv) 18 { 19 JavaVM* jvm; 20 JNIEnv* env; 21 22 jclass cls; 23 int ret = 0; 24 25 jmethodID mid; 26 jmethodID cid; 27 28 jobject jobj; 29 jstring jstr; 30 31 int r; 32 33 /* 1. create java virtual machine */ 34 if(create_vm(&jvm, &env)) 35 { 36 printf("can not create jvm\n"); 37 return -1; 38 } 39 40 /* 2. get class */ 41 cls = (*env)->FindClass(env, "Hello"); 42 if(cls == NULL) 43 { 44 printf("can not find hello class\n"); 45 ret = -1; 46 goto destory; 47 } 48 49 /* 3. create object 50 * 51 */ 52 53 cid = (*env)->GetMethodID(env, cls, "<init>", "()V"); 54 if(cid == NULL) 55 { 56 printf("can not get construct method\n"); 57 ret = -1; 58 goto destory; 59 } 60 61 jobj = (*env)->NewObject(env, cls, cid); 62 if(jobj == NULL) 63 { 64 printf("can not create object\n"); 65 ret = -1; 66 goto destory; 67 } 68 69 /* 4. call method 70 * 4.1 get method 71 * 4.2 create parameter 72 * 4.3 call method 73 */ 74 75 mid = (*env)->GetMethodID(env, cls, "sayhello_to", "(Ljava/lang/String;)I"); 76 if(mid == NULL) 77 { 78 ret = -1; 79 printf("can not get method\n"); 80 goto destory; 81 } 82 83 jstr = (*env)->NewStringUTF(env, "287787472@qq.com"); 84 85 r = (*env)->CallIntMethod(env, jobj, mid, jstr); 86 printf("ret = %d\n", r); 87 88 destory: 89 (*jvm)->DestroyJavaVM(jvm); 90 91 return ret; 92 }