简体   繁体   中英

Exception in thread “AWT-EventQueue-0” java.lang.NoSuchMethodError

I try to get jMethodID of the method in the java code ,but when I call this particular function, that does that an exception is thrown :

Exception in thread "AWT-EventQueue-0" java.lang.NoSuchMethodError: displayKeyStrokes
at org.suhail.keylogger.HelperClasses.NativeMethods.initializeJNIVars(Native Method)
at org.suhail.keylogger.GUI.MainGUI.jMenuItem1ActionPerformed(MainGUI.java:356)
.
.
.

I do not understand the reason for this. Following is the C code snippet that gets called from the java method :

void Java_org_suhail_keylogger_HelperClasses_NativeMethods_initializeJNIVars
(JNIEnv *env, jobject obj) {
 jclass cls = (*env)->GetObjectClass(env,obj);
 callBackToDeliverKeyStroke = (*env)->GetMethodID(env,cls,"displayKeyStrokes","()V");
 object = (*env)->NewGlobalRef(env,obj);
 if(object == NULL | callBackToDeliverKeyStroke == NULL | cls == NULL) {
    printf("Initialization error...One of the variable is Null\n");
 }
}

And this is the method named displayKeyStrokes in the java code :

 public void displayKeyStrokes() {
    System.out.println("Java Message : A Key has been pressed");
}

What could be the reason I am getting an exception ?

EDIT :

Java Code that calls JNI Code :

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
    jMenuItem1.setEnabled(false);
    jMenuItem2.setEnabled(true);
    try {
       System.loadLibrary("Dll_PKeylogger"); // Load the dll written to listen to the tapping of keys
       nativeMethods.initializeJNIVars(); // CALL
    }catch(Exception exc) {
        exc.printStackTrace();
    }
}

NativeMethods Class (declares the native methods)

package org.suhail.keylogger.HelperClasses;

public class NativeMethods {

 public native void initializeJNIVars();
 public native void unregisterHook(); 

}

NOTE : I am calling the method initializeJNIVars on the object of another class named NativeMethods and the method jMenuItem1ActionPerformed is called from an anonymous inner class whenever an event occurs as :

        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jMenuItem1ActionPerformed(evt);
        }
    });

You state:

jMenuItem1ActionPerformed is called from an anonymous class which is inside the same class as displayKeyStrokes. And yes jMenuItem1ActionPerformed is in the same class as jMenuItem1ActionPerformed except it is called from anonymous inner class as shown here

That's likely your problem then. The jMenuItem1ActionPerformed being in an anonymous inner class is important and means that it is in a different class from jMenuItem1ActionPerformed, and that this will need to be taken into account when you try to get the method ID from your JNI C code.

Consider giving your initializeJNIVars() method a parameter that takes an instance of the outer class, and then pass that instance into the parameter and use it when you call GetMethodID(...) . Note that you will have to pass the parameter in as OuterClassName.this .

The description of the exception you get is:

Thrown if an application tries to call a specified method of a class (either static or instance), and that class no longer has a definition of that method.

Normally, this error is caught by the compiler; this error can only occur at run time if the definition of a class has incompatibly changed.

Is your code thread separated? is there any chance the displayKeyStrokes method is loaded by a thread that is already dead when you try to call this method?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM