the first question is asked, so I will immediately accept it.
I have C code that will interact with Java; I did my JNI homework and topic. Here is the code:
C-part:
#include <stdio.h> #include <jni.h> #include <string.h> #define CLASSPATH "-Djava.class.path=/scratch/workareas/JTest/Java/" //folder which contains .class files #define DEBUG 0 JNIEnv* create_vm(JavaVM ** jvm) { JNIEnv *env; JavaVMInitArgs vm_args; JavaVMOption options; strcpy(options.optionString, CLASSPATH); //fix for options.optionString = CLASSPATH; if (DEBUG) printf("optionString = %s\n", options.optionString); vm_args.version = JNI_VERSION_1_6; //JDK version. This indicates version 1.6 vm_args.nOptions = 1; vm_args.options = &options; vm_args.ignoreUnrecognized = 0; int ret = JNI_CreateJavaVM(jvm, (void**) &env, &vm_args); if (ret < 0) printf("\n<<<<< Unable to Launch JVM >>>>>\n"); return env; } int main(int argc, char* argv[]) { JNIEnv* env; JavaVM* jvm; printf("Creating JVM...."); env = create_vm(&jvm); printf(" done! [env = %p\tjvm = %p]\n", env, jvm); if (env == NULL) return 1; jclass myClass = NULL; jmethodID dispatchMessage = NULL; int counter = 0; //Obtaining Class myClass = (*env)->FindClass(env, "EventHandler"); printf("FindClass done! [%p]\n", myClass); //Obtaining Method ID if (myClass != NULL) dispatchMessage = (*env)->GetStaticMethodID(env, myClass, "dispatchEvent", "(I)V"); else printf("Unable to find the requested class\n"); printf("Calling dispatchEvent() [%p] ...\n", dispatchMessage); if (dispatchMessage != NULL) { // jstring newMessage = (*env)->NewStringUTF(env, "Test call::Called from C\n"); for(counter = 0; counter < 10; counter ++) { jint newMessage = counter; (*env)->CallStaticVoidMethod(env, myClass, dispatchMessage, newMessage); } } printf("dispatchMessage() done!\n"); //Release resources. printf("Releasing resources..."); (*jvm)->DestroyJavaVM(jvm); printf(" done! Exiting.\n"); return 0; }
Java part:
public class EventHandler { public static final int EVENT_CODE_E1 = 1; public static final int EVENT_CODE_E2 = 2; //... public static final int EVENT_CODE_E10 = 10; private static EventHandler instance = new EventHandler(); public EventHandler() { //TODO: create object here } public static EventHandler getInstance() { if(instance == null) instance = new EventHandler(); return instance; } public static void dispatchEvent(int eventCode) { switch(eventCode) { case EVENT_CODE_E1: System.out.println("Event 1 firing!"); getInstance().onEventE1(); break; case EVENT_CODE_E2: System.out.println("Event 2 just fired!"); break; //case default: System.out.println("Unknown event with ID: "+eventCode+" triggered!"); break; } } private void onEventE1() { System.out.println("heyoooo"); } }
Now the question arises: after making the call 10 times, this is what I get as output:
Creating JVM.... done! [env = 0x80e6d20 jvm = 0x177e6a4] FindClass done! [0x80e7c78] Calling dispatchEvent() [0x90b3fe8c] ... Unknown event with ID: 0 triggered! Event 1 firing! heyoooo Event 2 just fired! Unknown event with ID: 3 triggered! Unknown event with ID: 4 triggered! Unknown event with ID: 5 triggered! Unknown event with ID: 6 triggered! Unknown event with ID: 7 triggered! Unknown event with ID: 8 triggered! Unknown event with ID: 9 triggered! dispatchMessage() done! Releasing resources... done! Exiting.
I have no idea what causes this crash. I looked through the magazine and received nothing meaningful. Can anybody help? :)
EDIT: I also pinned a magazine if someone noticed something interesting. I tried to cross reference it using objdump JNITest and tried to use addr2line, but that did not give me anything. The GDB session was also ineffective in determining the root cause.
#
Thanks for all the efforts :)