韦东山视频第3课第2节_JNI_C调用JAVA_P【学习笔记】

C调JAVA方法主要步骤如下:

 

一、C代码调用java的静态方法

Hello.java

 1 public class Hello{
 2         public static void main(String args[]){
 3                 System.out.println("Hello World!");
 4         }
 5 
 6         public static void sayhello_to(String name){
 7 
 8         }
 9 
10         public static void sayhello_to(){
11 
12         }
13 
14 }

caller.c

 1 #include <stdio.h>  
 2 #include <jni.h> 
 3 
 4 
 5 jint 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 
18 int main(int argc, char **argv)
19 {
20     JavaVM* jvm;
21     JNIEnv* env;
22 
23     jclass cls;
24     int ret = 0;
25 
26     jmethodID mid;
27         
28     if (create_vm(&jvm, &env)) {
29         printf("can not create jvm\n");
30         return -1;
31     }
32 
33     cls = (*env)->FindClass(env, "Hello");
34     if (cls == NULL) {
35         printf("can not find hello class\n");
36         ret = -1;
37         goto destroy;
38     }
39     
40     mid = (*env)->GetStaticMethodID(env, cls, "main","([Ljava/lang/String;)V");
41     if (mid == NULL) {
42         ret = -1;
43         printf("can not get method\n");
44         goto destroy;
45     }
46 
47     (*env)->CallStaticVoidMethod(env, cls, mid, NULL);
48 
49 destroy:
50 
51     (*jvm)->DestroyJavaVM(jvm);
52     return ret;
53 }

编译运行命令:

1 javac Hello.java
2 
3 javap -p -s Hello.class 
4 
5 gcc -I/usr/lib/jvm/java-1.7.0-openjdk-amd64/include/ -o caller caller.c -L /usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/server -ljvm
6 
7 LD_LIBRARY_PATH=/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/server ./caller

 

 二、C调java非静态方法

Hello.java

 1 public class Hello{
 2     public static void main(String args[]){
 3         System.out.println("Hello World!");
 4     }
 5 
 6     public int sayhello_to(String name){
 7         System.out.println("java Hello World! " + name);
 8         return 123;
 9     }
10 
11     public static void sayhello_to(){
12     
13     }
14     
15 }

caller.c

 1 #include <stdio.h>    
 2 #include <jni.h> 
 3 
 4 
 5 jint 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 
18 int main(int argc, char **argv)
19 {
20     JavaVM* jvm;
21     JNIEnv* env;
22 
23     jclass cls;
24     int ret = 0;
25 
26     jmethodID mid;
27     jmethodID cid;
28 
29     jobject jobj;
30     jstring jstr;
31 
32     int r;
33         
34     if (create_vm(&jvm, &env)) {
35         printf("can not create jvm\n");
36         return -1;
37     }
38 
39     cls = (*env)->FindClass(env, "Hello");
40     if (cls == NULL) {
41         printf("can not find hello class\n");
42         ret = -1;
43         goto destroy;
44     }
45 
46     cid = (*env)->GetMethodID(env, cls, "<init>", "()V");
47     if (cid == NULL) {
48         ret = -1;
49         printf("can not get constructor method");
50         goto destroy;
51     }
52 
53     jobj = (*env)->NewObject(env, cls, cid);
54     if (jobj == NULL) {
55         ret = -1;
56         printf("can not create object");
57         goto destroy;
58     }
59 
60     mid = (*env)->GetMethodID(env, cls, "sayhello_to","(Ljava/lang/String;)I");
61     if (mid == NULL) {
62         ret = -1;
63         printf("can not get method\n");
64         goto destroy;
65     }
66 
67     jstr = (*env)->NewStringUTF(env, "weidongshan@qq.com");
68 
69     r = (*env)->CallIntMethod(env, jobj, mid, jstr);
70     printf("jni ret = %d\n", r);
71 
72 destroy:
73 
74     (*jvm)->DestroyJavaVM(jvm);
75     return ret;
76 }

运行结果:

 

 二、C调java非静态方法,并设置属性

Hello.java

 1 public class Hello{
 2     private String name;
 3     private int age;
 4 
 5     public static void main(String args[]){
 6         System.out.println("Hello World!");
 7     }
 8 
 9     public int sayhello_to(String name){
10         System.out.println("java Hello , " + name + " I'm " + this.name + "," + age+"years old");
11         return 123;
12     }
13 
14     public static void sayhello_to(){
15     
16     }
17     
18 }

caller.c

 1 #include <stdio.h>  
 2 #include <jni.h> 
 3 
 4 
 5 jint 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 
18 int main(int argc, char **argv)
19 {
20     JavaVM* jvm;
21     JNIEnv* env;
22 
23     jclass cls;
24     int ret = 0;
25 
26     jmethodID mid;
27     jmethodID cid;
28 
29     jobject jobj;
30     jstring jstr;
31 
32     jfieldID nameID;
33     jfieldID ageID;
34 
35     int r;
36         
37     if (create_vm(&jvm, &env)) {
38         printf("can not create jvm\n");
39         return -1;
40     }
41 
42     cls = (*env)->FindClass(env, "Hello");
43     if (cls == NULL) {
44         printf("can not find hello class\n");
45         ret = -1;
46         goto destroy;
47     }
48 
49     cid = (*env)->GetMethodID(env, cls,    "<init>", "()V");
50     if (cid == NULL) {
51         ret = -1;
52         printf("can not get constructor method");
53         goto destroy;
54     }
55 
56     jobj = (*env)->NewObject(env, cls, cid);
57     if (jobj == NULL) {
58         ret = -1;
59         printf("can not create object");
60         goto destroy;
61     }
62 
63     nameID = (*env)->GetFieldID(env, cls, "name", "Ljava/lang/String;");
64     if (nameID == NULL) {
65         ret = -1;
66         printf("can not get field name");
67         goto destroy;
68     }
69     jstr = (*env)->NewStringUTF(env, "Bill");
70     (*env)->SetObjectField(env, jobj, nameID, jstr);
71 
72     ageID = (*env)->GetFieldID(env, cls, "age", "I");
73     if (ageID == NULL) {
74         ret = -1;
75         printf("can not get field age");
76         goto destroy;
77     }
78     (*env)->SetIntField(env, jobj, ageID, 10);
79 
80     mid = (*env)->GetMethodID(env, cls, "sayhello_to","(Ljava/lang/String;)I");
81     if (mid == NULL) {
82         ret = -1;
83         printf("can not get method\n");
84         goto destroy;
85     }
86 
87     jstr = (*env)->NewStringUTF(env, "weidongshan@qq.com");
88 
89     r = (*env)->CallIntMethod(env, jobj, mid, jstr);
90     printf("ret = %d\n", r);
91 
92 destroy:
93 
94     (*jvm)->DestroyJavaVM(jvm);
95     return ret;
96 }

运行结果:

 

 相关代码可以在这里下载:

 https://github.com/zzb2760715357/100ask/tree/master/java

 

posted @ 2019-07-07 18:12  请给我倒杯茶  阅读(445)  评论(0编辑  收藏  举报