C调用JAVA

  1. #include <jni.h>   
  2. #include <stdlib.h>   
  3. #include <stdio.h>   
  4. int main(int argc, char*argv[]) {   
  5.   
  6.   JavaVM *jvm;   
  7.    JNIEnv *env;   
  8.      JavaVMInitArgs vm_args;   
  9.      JavaVMOption options[1];   
  10.   
  11.      jobjectArray applicationArgs;   
  12.      jstring appArg;   
  13.   
  14.      /*  
  15.       * Setting VM arguments  
  16.       */  
  17.      vm_args.version = JNI_VERSION_1_2;   
  18.      vm_args.ignoreUnrecognized = JNI_TRUE;   
  19.      vm_args.nOptions = 0;   
  20.   
  21.      /*  
  22.       * Setting classpath  
  23.       */  
  24.      char classpath[1024] = "-Djava.class.path=";   
  25.      char *env_classpath = getenv("CLASSPATH");   
  26.   
  27.      int mainclass_index = 1;   
  28.      if (argc >= 3 && !strcmp("-classpath", argv[1])) {   
  29.          options[0].optionString = strcat(classpath, argv[2]);   
  30.          vm_args.nOptions++;   
  31.          mainclass_index += 2;   
  32.      } else if (env_classpath) {   
  33.          options[0].optionString = strcat(classpath, env_classpath);   
  34.          vm_args.nOptions++;   
  35.      }   
  36.   
  37.      if (vm_args.nOptions > 0) {   
  38.          vm_args.options = options;   
  39.      }   
  40.   
  41.      if (mainclass_index >= argc) {   
  42.          printf("Main class not found, please specify it/n");   
  43.          return 0;   
  44.      }   
  45.   
  46.      jint res = JNI_CreateJavaVM(&jvm, (void **)&env, &vm_args);   
  47.      if (res < 0) {   
  48.          printf("Create Java VM error, code = %d/n", res);   
  49.          return -1;   
  50.      }   
  51.   
  52.      jclass cls = (*env)->FindClass(env, argv[mainclass_index]);   
  53.      if (!cls) {   
  54.          printf("Class %s not found/n", argv[mainclass_index]);   
  55.          return -1;   
  56.      }   
  57.   
  58.      jmethodID mid = (*env)->GetStaticMethodID(env, cls, "main""([Ljava/lang/String;)V");   
  59.   
  60.      if (!mid) {   
  61.          printf("Method %s of Class %s not found/n""main", argv[mainclass_index]);   
  62.          return -1;   
  63.      }   
  64.      applicationArgs = (*env)->NewObjectArray(env, argc - mainclass_index - 1,   
  65.                                               (*env)->FindClass(env, "java/lang/String"),   
  66.                                               NULL);   
  67.   
  68.      int i = 0;   
  69.      for (i = mainclass_index + 1; i < argc; i ++) {   
  70.          appArg = (*env)->NewStringUTF(env, argv[i]);   
  71.          (*env)->SetObjectArrayElement(env, applicationArgs, i - mainclass_index - 1, appArg);   
  72.      }   
  73.   
  74.      (*env)->CallStaticVoidMethod(env, cls, mid, applicationArgs);   
  75.   
  76.      printf("before destroy/n");   
  77.   
  78.      /*  
  79.       * Destroy the JVM.  
  80.       * This is necessary, otherwise if the called method exits,  
  81.       * this program will return immediately.  
  82.       */  
  83.      (*jvm)->DestroyJavaVM(jvm);   
  84.   
  85.      printf("after destroy/n");   
  86.   
  87.      return 0;   
  88.  }  

 

二、编译,书写makefile文件 ,注意要链接JDK中所自带的jvm.so文件,“-L/sandbox/JAVA2S/jdk1.6.0_16/jre/lib/i386/client/”

 

  1. all:jvm   
  2. jvm:   
  3.     gcc -o jvm jvm.c -I/sandbox/JAVA2S/jdk1.6.0_16/include -I/sandbox/JAVA2S/jdk1.6.0_16/include/linux/ -L/sandbox/JAVA2S/jdk1.6.0_16/jre/lib/i386/client/ -ljvm  

 

三、修改.bash_profile文件,设置环境变量,即libjvm.so 所在的路径

export LD_LIBRARY_PATH=/sandbox/JAVA2S/jdk1.6.0_16/jre/lib/i386/client

四、运行命令./jvm -classpath . Test 34 67

结果如下

至此,C语言调用Java程序完成

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值