1

I want to pass a treemap from java to c with jni. I call the native method:

public native int getEuklid(TreeMap<int,short[]> map);
static {
 System.loadLibrary("Euklid");
}

Now in my c file i want to access the elements in the map. How i can do that. This is the header from my c file:

JNIEXPORT int JNICALL 
Java_Main_getEuklid (JNIEnv *env, jobject o, jobject o2)

Where o2 is the treemap. How can i handle with that jobject like the treemap in java?

Edit:

Where is my fault?:

JNIEXPORT int JNICALL 
Java_Main_getEuklid (JNIEnv *env, jobject o, jobject o2){
 jclass cls = env->GetObjectClass(o2) ; 
 jmethodID mGetValue = env->GetMethodID(cls, "keySet","()Ljava/util/Set;"); 
 jobject value = env->CallObjectMethod(o2, mGetValue)

I became:

In function ‘Java_Main_getEuklid’:
11: error: request for member ‘GetObjectClass’ in something not a structure or union
12: error: request for member ‘GetMethodID’ in something not a structure or union
13: error: request for member ‘CallObjectMethod’ in something not a structure or union

I am really new in c and i want to test something so i hope you could help me.

bladepit
  • 853
  • 5
  • 14
  • 29
  • Related:http://stackoverflow.com/questions/4844022/jni-create-hashmap and http://stackoverflow.com/questions/5641499/making-generic-calls-with-java-jni-and-c – Cratylus Apr 21 '13 at 17:38

1 Answers1

2

I think it is pure C code. You should write something like:

(*env)->GetObjectClass(env, o2)

In other words, RTFM, start here http://en.wikipedia.org/wiki/Java_Native_Interface .

kan
  • 28,279
  • 7
  • 71
  • 101
  • ok thank you that will fix my problem above. i have another question: i compile my test programm with: gcc -Wall -O10 -o test test.c. It needs not longer than one second. If i test my created library it is very slow. I create the library with: gcc -O10 -I/System/Library/Frameworks/JavaVM.framework/Headers -c -o Euklid.o Euklid.c and then gcc -O10 -dynamiclib -o libeuklid.dylib Euklid.o....why is it so slow when i am working with the lib? – bladepit Apr 22 '13 at 07:36
  • 1
    @bladepit I don't know, maybe it's better to raise another question. I suspect it happens because the lib compilation uses JDK headers and libs a lot, it could be quite massive files. Try with no optimization (`-O10`), it is fine during development. – kan Apr 23 '13 at 11:15