JNI -- getting AbstractMethodError

J

Jeff Gaynor

Hi,

I am writing a small JNI program. Now, at this point almost the only thing
that it does is to accept a java.io.File object then read the abstract
pathname (the whole program is just a couple of lines, included below). I get
that java throws and AbstractMethodError. I can't figure out why this might
be the case -- getAbstractPath is not abstract. Can anyone help here?

Thanks in Advance,

-- Jeff

======

Note: * PSZ is a pointer to a null-terminated string.
* I am not a C programmer, but am trying my hand at this. Advice/tips
appreciated!
* This is a utility and should just grab the name from a file that is
passed and return
it.

PSZ getFileName(JNIEnv *env, jobject jFile){
/* Variable declarations */
char buffer[256];
jclass fileClass = NULL;
jmethodID mid = NULL;
jstring jfName = NULL;
const char *fName = 0;
/* now for some values */
fileClass = (*env)->FindClass(env, "Ljava/io/File;");
printf("got fileClass\n");
mid = (*env)->GetMethodID(env, fileClass,"getAbsolutePath",
"()Ljava/lang/String;");
printf("got method id\n");
/* it dies right after this print statement... */
jfName = (*env)->CallObjectMethod(env,fileClass,mid);
if(jfName == NULL){
printf("jfName is null\n");
return NULL;
}
printf("got jfName\n");
fName = (*env)->GetStringUTFChars(env, jfName, 0);
printf("copied file name:%s\n", fName);

strcpy(buffer, fName);
(*env)->ReleaseStringUTFChars(env, jfName, fName);
printf("file name = %s\n", buffer);
return buffer;
}
 
G

Gordon Beaton

I get that java throws and AbstractMethodError. I can't figure out
why this might be the case -- getAbstractPath is not abstract.
[...]

PSZ getFileName(JNIEnv *env, jobject jFile){
/* Variable declarations */
char buffer[256];
jclass fileClass = NULL;
jmethodID mid = NULL;
jstring jfName = NULL;
const char *fName = 0;
/* now for some values */
fileClass = (*env)->FindClass(env, "Ljava/io/File;");

The argument to FindClass should be "java/io/File". I believe that
some JVMs also accept the "signature" format you've used here, but not
all do, and this format isn't the one used in the Sun sources.

Note that you could also use GetObjectClass(env,jFile) in this case,
and avoid the issue altogether.
printf("got fileClass\n");
mid = (*env)->GetMethodID(env, fileClass,"getAbsolutePath",
"()Ljava/lang/String;");
printf("got method id\n");

I suggest that you check for NULL after FindClass() and again after
GetMethodID() before proceeding. Whenever a JNI function fails, you
can call this to see why:

if ((*env)->ExceptionOccurred(env)) {
(*env)->ExceptionDescribe(env);
}
/* it dies right after this print statement... */
jfName = (*env)->CallObjectMethod(env,fileClass,mid);

Here you are attempting to call an instance method on the class
itself. The second argument should be jFile, not fileClass.
if(jfName == NULL){
printf("jfName is null\n");
return NULL;
}
printf("got jfName\n");
fName = (*env)->GetStringUTFChars(env, jfName, 0);
printf("copied file name:%s\n", fName);

strcpy(buffer, fName);
(*env)->ReleaseStringUTFChars(env, jfName, fName);
printf("file name = %s\n", buffer);
return buffer;
}

/gordon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top