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:
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 callGetMethodID(...). Note that you will have to pass the parameter in asOuterClassName.this.