Last active
September 24, 2016 08:53
-
-
Save leonardosnt/9df28af41ead3f8f240ca3718e00ff8a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include "jni.h" | |
void nativeFoo(); | |
int main(void) { | |
JNIEnv* env; | |
JavaVM* jvm; | |
JavaVMInitArgs vm_args; | |
JavaVMOption* options = new JavaVMOption[1]; | |
options->optionString = "-Djava.class.path=."; | |
vm_args.version = JNI_VERSION_1_8; | |
vm_args.options = options; | |
vm_args.nOptions = 1; | |
vm_args.ignoreUnrecognized = JNI_FALSE; | |
jint i = JNI_CreateJavaVM(&jvm, (void**) &env, &vm_args); | |
printf("> Env 0x%X\n", env); | |
jclass cls = env->FindClass("Foo"); | |
const JNINativeMethod mds[] = { | |
{ "foo", "()V", (void*) nativeFoo } | |
}; | |
env->RegisterNatives(cls, mds, 1); | |
printf("> Cls 0x%X\n", &cls); | |
jmethodID mid = env->GetStaticMethodID(cls, "bar", "()V"); | |
printf("> Mid 0x%X\n", &mid); | |
env->CallStaticVoidMethod(cls, mid); | |
jvm->DestroyJavaVM(); | |
delete[] options; | |
} | |
void nativeFoo() { | |
printf("Called nativeFoo\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment