I have a JNI function in C which is getting passed in a Java FILE class which represents a directory listing. I would like to call the list() function and get the list of strings (files in the directory). What is the best way to do this?
Right now I have
static void* my_function(JNIEnv *env, jobject obj, jobject dir){
jarray listRet;
jclass cls = (*env)->GetObjectClass(env, dir);
jmethodID method = (*env)->GetMethodID(env, cls, "list", "()[Ljava/lang/String");
listRet = (*env)->CallObjectMethod(env, cls, method);
jsize stringCount = (*env)->GetArrayLength(env, listRet);
}
However, by adding logging statements, it seems that it never gets past the GetObjectClass call. So, is this call correct? Further, is the GetMethodID call correct? The return type of list() is a (java) String[]. Is there anywhere else that I'm going wrong?
list
is not a static method of File
. That is, it belongs to an instance of File
(dir
in your case), not to the File
class.
So instead of:
listRet = (*env)->CallObjectMethod(env, cls, method);
you should be using:
listRet = (*env)->CallObjectMethod(env, dir, method);
Also, you seem to be missing a semicolon in the signature for list
. It should be "()[Ljava/lang/String;"