我尝试jMethodID
在 java 代码中获取该方法,但是当我调用这个特定函数时,会引发异常:
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)
.
.
.
我不明白这是为什么。以下是从 java 方法调用的C代码片段:
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");
}
}
这是displayKeyStrokes
java代码中命名的方法:
public void displayKeyStrokes() {
System.out.println("Java Message : A Key has been pressed");
}
我遇到异常的原因可能是什么?
编辑 :
调用 JNI 代码的 Java 代码:
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 类(声明本机方法)
package org.suhail.keylogger.HelperClasses;
public class NativeMethods {
public native void initializeJNIVars();
public native void unregisterHook();
}
注意:我在initializeJNIVars
另一个名为的类的对象上NativeMethods
并且jMenuItem1ActionPerformed
每当事件发生时从匿名内部类调用该方法:
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem1ActionPerformed(evt);
}
});