Skip to content

Instantly share code, notes, and snippets.

@leonardosnt
Last active September 24, 2016 08:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leonardosnt/9df28af41ead3f8f240ca3718e00ff8a to your computer and use it in GitHub Desktop.
Save leonardosnt/9df28af41ead3f8f240ca3718e00ff8a to your computer and use it in GitHub Desktop.
#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