gpt4 book ai didi

java - 为什么 jvm 在我回拨 java 函数时崩溃?

转载 作者:太空宇宙 更新时间:2023-11-04 06:04:39 26 4
gpt4 key购买 nike

以下 java 片段调用 jni 函数 Java_org_suhail_keylogger_HelperClasses_NativeMethods_unregisterHook :

    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(); // called upon the object of a class named NativeMethods
}catch(Exception exc) {
exc.printStackTrace();
}
}

NativeMethods(类,其对象用于调用上面的 JNI C 方法):

public class NativeMethods {

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

public void displayKeyStrokes() { // FUNCTION THAT IS CALLED BACK FROM JNI C CODE
System.out.println("Java Message : A Key has been pressed");
}
}

JNI C 方法,由 java 代码调用:

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");
}
}

与上述调用java函数的方法在同一模块的方法:

static LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
JNIEnv *Env;
(*Env)->CallVoidMethod(Env,object,callBackToDeliverKeyStroke);
// I have initialized object and callBackToDeliverKeyStroke in the above method
}

当执行到达最后一个执行点时,即上面提到的函数 JVM 崩溃。这是为什么 ?我在哪里犯了错误?

最佳答案

JNIEnv *Env;
(*Env)->CallVoidMethod(Env,object,callBackToDeliverKeyStroke);

Env 未初始化。

关于java - 为什么 jvm 在我回拨 java 函数时崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12729430/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com