java高级编程,JNI的使用。c程序调用java代码

JNI不仅可以让我们在java中调用C语言程序,而且 在c中同样可以调用java'程序


C/C++代码调用java代码的而情况:

1  实现化平台使用

2  访问java语言编写的代码或者代码库

3 希望利用标准的 java类库

如何来实现呢?

一 、创建一个C语言程序‘



#include <stdio.h>  
#include <jni.h> 




jint create_vm(JavaVM** jvm, JNIEnv** env) 
{  
    JavaVMInitArgs args;  
    JavaVMOption options[1];  
    args.version = JNI_VERSION_1_6;  
    args.nOptions = 1;  
    options[0].optionString = "-Djava.class.path=./";  
    args.options = options;  
    args.ignoreUnrecognized = JNI_FALSE;  
    return JNI_CreateJavaVM(jvm, (void **)env, &args);  
}  

int main(int argc, char **argv)
{
JavaVM* jvm;
JNIEnv* env;


jclass cls;
int ret = 0;

jmethodID mid;

/* 1. create java virtual machine */
if (create_vm(&jvm, &env)) {
printf("can not create jvm\n");
return -1;
}


/* 2. get class */
cls = (*env)->FindClass(env, "Hello");
if (cls == NULL) {
printf("can not find hello class\n");
ret = -1;
goto destroy;
}


/* 3. create object */


/* 4. call method
* 4.1 get method
* 4.2 create parameter
* 4.3 call method
*/


mid = (*env)->GetStaticMethodID(env, cls, "main","([Ljava/lang/String;)V");
if (mid == NULL) {
ret = -1;
printf("can not get method\n");
goto destroy;
}


(*env)->CallStaticVoidMethod(env, cls, mid, NULL);


destroy:


(*jvm)->DestroyJavaVM(jvm);
return ret;
}


2 java程序

public class Hello {
public static void main(String args[]) {
System.out.println("Hello, world!");
}


    public static void sayhello_to(String name) {
    }


    public static void sayhello_to() {
    }
}


编译运行

book@book-virtual-machine:/work/javaproject/18th_jni/06th_c_call_java/01th_call_static_method$ javap -p -s Hello.class 
Compiled from "Hello.java"
public class Hello {
  public Hello();
    Signature: ()V


  public static void main(java.lang.String[]);
    Signature: ([Ljava/lang/String;)V


  public static void sayhello_to(java.lang.String);
    Signature: (Ljava/lang/String;)V


  public static void sayhello_to();
    Signature: ()V
}

book@book-virtual-machine:/work/javaproject/18th_jni/06th_c_call_java/01th_call_static_method$ javac Hello.java 
book@book-virtual-machine:/work/javaproject/18th_jni/06th_c_call_java/01th_call_static_method$ 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
book@book-virtual-machine:/work/javaproject/18th_jni/06th_c_call_java/01th_call_static_method$ LD_LIBRARY_PATH=/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/server ./caller
Hello, world!
book@book-virtual-machine:/work/javaproject/18th_jni/06th_c_call_java/01th_call_static_method$ 

二 、


c程序:



#include <stdio.h>  
#include <jni.h> 




jint create_vm(JavaVM** jvm, JNIEnv** env) 
{  
    JavaVMInitArgs args;  
    JavaVMOption options[1];  
    args.version = JNI_VERSION_1_6;  
    args.nOptions = 1;  
    options[0].optionString = "-Djava.class.path=./";  
    args.options = options;  
    args.ignoreUnrecognized = JNI_FALSE;  
    return JNI_CreateJavaVM(jvm, (void **)env, &args);  
}  




int main(int argc, char **argv)
{
JavaVM* jvm;
JNIEnv* env;


jclass cls;
int ret = 0;


jmethodID mid;
jmethodID cid;


jobject jobj;
jstring jstr;


int r;

/* 1. create java virtual machine */
if (create_vm(&jvm, &env)) {
printf("can not create jvm\n");
return -1;
}


/* 2. get class */
cls = (*env)->FindClass(env, "Hello");
if (cls == NULL) {
printf("can not find hello class\n");
ret = -1;
goto destroy;
}


/* 3. create object 
* 3.1 get constructor method
* 3.2 create parameters
* 3.3 NewObject
*/


/* Get the method ID for the String constructor */
cid = (*env)->GetMethodID(env, cls,"<init>", "()V");
if (cid == NULL) {
ret = -1;
printf("can not get constructor method");
goto destroy;
}


jobj = (*env)->NewObject(env, cls, cid);
if (jobj == NULL) {
ret = -1;
printf("can not create object");
goto destroy;
}


/* 4. call method
* 4.1 get method
* 4.2 create parameter
* 4.3 call method
*/


mid = (*env)->GetMethodID(env, cls, "sayhello_to","(Ljava/lang/String;)I");
if (mid == NULL) {
ret = -1;
printf("can not get method\n");
goto destroy;
}


jstr = (*env)->NewStringUTF(env, "http://blog.youkuaiyun.com/joshua_love");


r = (*env)->CallIntMethod(env, jobj, mid, jstr);
printf("ret = %d\n", r);


destroy:


(*jvm)->DestroyJavaVM(jvm);
return ret;
}



java程序:


public class Hello {
public static void main(String args[]) {
System.out.println("Hello, world!");
}


    public int sayhello_to(String name) {
System.out.println("Hello, "+name);       
        return 123;
    }


    public static void sayhello_to() {
    }
}

book@book-virtual-machine:/work/javaproject/18th_jni/06th_c_call_java/02th_call_non_static_metod$ javac Hello.java 
book@book-virtual-machine:/work/javaproject/18th_jni/06th_c_call_java/02th_call_non_static_metod$ javap -p -s Hello.class 
Compiled from "Hello.java"
public class Hello {
  public Hello();
    Signature: ()V


  public static void main(java.lang.String[]);
    Signature: ([Ljava/lang/String;)V


  public int sayhello_to(java.lang.String);
    Signature: (Ljava/lang/String;)I


  public static void sayhello_to();
    Signature: ()V
}
book@book-virtual-machine:/work/javaproject/18th_jni/06th_c_call_java/02th_call_non_static_metod$ 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
book@book-virtual-machine:/work/javaproject/18th_jni/06th_c_call_java/02th_call_non_static_metod$ LD_LIBRARY_PATH=/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/server ./caller
Hello, http://blog.youkuaiyun.com/joshua_love
ret = 123
book@book-virtual-machine:/work/javaproject/18th_jni/06th_c_call_java/02th_call_non_static_metod$ 

三、

c程序:



#include <stdio.h>  
#include <jni.h> 




jint create_vm(JavaVM** jvm, JNIEnv** env) 
{  
    JavaVMInitArgs args;  
    JavaVMOption options[1];  
    args.version = JNI_VERSION_1_6;  
    args.nOptions = 1;  
    options[0].optionString = "-Djava.class.path=./";  
    args.options = options;  
    args.ignoreUnrecognized = JNI_FALSE;  
    return JNI_CreateJavaVM(jvm, (void **)env, &args);  
}  




int main(int argc, char **argv)
{
JavaVM* jvm;
JNIEnv* env;


jclass cls;
int ret = 0;


jmethodID mid;
jmethodID cid;


jobject jobj;
jstring jstr;


jfieldID nameID;
jfieldID ageID;


int r;

/* 1. create java virtual machine */
if (create_vm(&jvm, &env)) {
printf("can not create jvm\n");
return -1;
}


/* 2. get class */
cls = (*env)->FindClass(env, "Hello");
if (cls == NULL) {
printf("can not find hello class\n");
ret = -1;
goto destroy;
}


/* 3. create object 
* 3.1 get constructor method
* 3.2 create parameters
* 3.3 NewObject
*/


/* Get the method ID for the String constructor */
cid = (*env)->GetMethodID(env, cls,"<init>", "()V");
if (cid == NULL) {
ret = -1;
printf("can not get constructor method");
goto destroy;
}


jobj = (*env)->NewObject(env, cls, cid);
if (jobj == NULL) {
ret = -1;
printf("can not create object");
goto destroy;
}


/* get/set field
* 1. get field id
* 2. get/set field
*/


nameID = (*env)->GetFieldID(env, cls, "name", "Ljava/lang/String;");
if (nameID == NULL) {
ret = -1;
printf("can not get field name");
goto destroy;
}
jstr = (*env)->NewStringUTF(env, "Bill");
(*env)->SetObjectField(env, jobj, nameID, jstr);


ageID = (*env)->GetFieldID(env, cls, "age", "I");
if (ageID == NULL) {
ret = -1;
printf("can not get field age");
goto destroy;
}
(*env)->SetIntField(env, jobj, ageID, 10);


/* 4. call method
* 4.1 get method
* 4.2 create parameter
* 4.3 call method
*/


mid = (*env)->GetMethodID(env, cls, "sayhello_to","(Ljava/lang/String;)I");
if (mid == NULL) {
ret = -1;
printf("can not get method\n");
goto destroy;
}


jstr = (*env)->NewStringUTF(env, "http://blog.youkuaiyun.com/joshua_love");


r = (*env)->CallIntMethod(env, jobj, mid, jstr);
printf("ret = %d\n", r);


destroy:


(*jvm)->DestroyJavaVM(jvm);
return ret;
}


java程序:





public class Hello {
    private String name;
    private int age;
    
public static void main(String args[]) {
System.out.println("Hello, world!");
}


    public int sayhello_to(String name) {
System.out.println("Hello, "+name+"! I am "+this.name+", "+age+" years old.");       
        return 123;
    }


    public static void sayhello_to() {
    }
}


book@book-virtual-machine:/work/javaproject/18th_jni/06th_c_call_java/03th_set_field$ javac Hello.java 
book@book-virtual-machine:/work/javaproject/18th_jni/06th_c_call_java/03th_set_field$ javap -p -s Hello.class 
Compiled from "Hello.java"
public class Hello {
  private java.lang.String name;
    Signature: Ljava/lang/String;
  private int age;
    Signature: I
  public Hello();
    Signature: ()V


  public static void main(java.lang.String[]);
    Signature: ([Ljava/lang/String;)V


  public int sayhello_to(java.lang.String);
    Signature: (Ljava/lang/String;)I


  public static void sayhello_to();
    Signature: ()V
}
book@book-virtual-machine:/work/javaproject/18th_jni/06th_c_call_java/03th_set_field$ 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
book@book-virtual-machine:/work/javaproject/18th_jni/06th_c_call_java/03th_set_field$ LD_LIBRARY_PATH=/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/server ./caller
Hello, http://blog.youkuaiyun.com/joshua_love! I am Bill, 10 years old.
ret = 123
book@book-virtual-machine:/work/javaproject/18th_jni/06th_c_call_java/03th_set_field$ 


















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值