温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C语言调用java的方法

发布时间:2020-07-01 11:01:25 来源:亿速云 阅读:321 作者:Leah 栏目:编程语言

本篇文章为大家展示了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

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

JNIEnv* 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 destory;
 }

 /* 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 destory;
 }

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

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

(2)call_non_static_method

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

JNIEnv* 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 destory;
}

/* 3. create object 
*  
*/

cid = (*env)->GetMethodID(env, cls, "<init>", "()V");
if(cid == NULL)
{
 printf("can not get construct method\n");
 ret = -1;
 goto destory;
}

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

/* 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 destory;
}

jstr = (*env)->NewStringUTF(env, "287787472@qq.com");

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

destory:
(*jvm)->DestroyJavaVM(jvm);

return ret;
}

上述内容就是C语言调用java的方法,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI