首页 > Java > Java入门 > c如何调用java

c如何调用java

爱喝马黛茶的安东尼
发布: 2019-11-11 13:33:44
原创
2761 人浏览过

c如何调用java

步骤:

1. 创建虚拟机;

2. 获得class;

3. 实例化对象:获得构造方法(方法名为“”),构造参数,调用方法。

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;
}
登录后复制

php中文网,大量的免费Java入门教程,欢迎在线学习!

以上是c如何调用java的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板