Skip to content

Instantly share code, notes, and snippets.

@HungMingWu
Created November 8, 2012 08:16
Show Gist options
  • Save HungMingWu/4037503 to your computer and use it in GitHub Desktop.
Save HungMingWu/4037503 to your computer and use it in GitHub Desktop.
JNI Tutorial 2
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <jni.h>
int main (int argc, char *argv[])
{
JavaVM *jvm; /* 宣告一個java machine */
JNIEnv *env; /* JNI的環境 */
JavaVMInitArgs vm_args;
/*自定義JRE所要的參數,就是java -... -... xxx.java將-...字串加入options中 用*/
JavaVMOption options[1];
jint res;
jclass cls;
jmethodID mid;
vm_args.version = JNI_VERSION_1_6; /*使用的版本*/
options[0].optionString = "-Djava.class.path=./";
vm_args.nOptions = 1;
vm_args.options = options;
vm_args.ignoreUnrecognized = JNI_FALSE;
/*產生一個java machine*/
res = JNI_CreateJavaVM(&jvm,(void**)&env,&vm_args);
jclass ver;
jmethodID print;
/*找Helloworld Class*/
cls = env->FindClass("HelloWorld");
if (cls == 0) printf("cls error \n");
/*>確定要執行的 function是甚麼
在這裡指的是HelloWorld裡的main
後面是main要傳的參數*/
mid = env->GetStaticMethodID(cls, "main", "([Ljava/lang/String;)V");
if (mid == 0) printf("mid error \n");
env->CallStaticVoidMethod(cls, mid); /*執行此class*/
printf("Hello in c\n");
return 0;
}
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World in Java");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment