JNI -- getting AbstractMethodError

Discussion in 'Java' started by Jeff Gaynor, May 25, 2004.

  1. Jeff Gaynor

    Jeff Gaynor Guest

    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;
    }
     
    Jeff Gaynor, May 25, 2004
    #1
    1. Advertising

  2. On Tue, 25 May 2004 09:16:10 -0500 (CDT), Jeff Gaynor wrote:

    > 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

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, May 25, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bin Xin
    Replies:
    3
    Views:
    4,518
    John C. Bollinger
    Aug 18, 2003
  2. Alex Hunsley

    IBM's JNI fails where Sun's JNI works

    Alex Hunsley, Nov 3, 2003, in forum: Java
    Replies:
    4
    Views:
    851
    Alex Hunsley
    Nov 4, 2003
  3. Replies:
    1
    Views:
    933
    Lee Fesperman
    May 19, 2005
  4. Replies:
    2
    Views:
    5,491
  5. Oliver Wong
    Replies:
    11
    Views:
    1,014
Loading...

Share This Page