You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JNI中创建Java对象触发JVM崩溃问题求助

Fixing JVM Crash when Creating Java Object from C++ via JNI

That EXCEPTION_ACCESS_VIOLATION crash is almost certainly happening because you're operating on an invalid jclass reference. Let's break down the problem and fix it step by step:

Root Cause

Your FindClass("ConstructorObject") call is likely returning NULL (meaning the JVM couldn't locate the class), but you're proceeding to call GetMethodID and NewObject with this invalid pointer. This triggers a memory access error because the JVM tries to dereference a null pointer when working with the invalid class.

The most common reason FindClass fails here is that you're not using the fully qualified class name with slashes instead of dots. For example, if your ConstructorObject class is in the same main package as NativeNewObject, you need to pass "main/ConstructorObject" instead of just "ConstructorObject".

Fixed C++ Code

Here's the corrected code with proper error checking (always validate JNI calls—this will save you tons of debugging time):

JNIEXPORT void JNICALL Java_main_NativeNewObject_callConstructorObject0(JNIEnv *e, jobject obj) {
    // 1. Find the class with the correct fully qualified name
    jclass c = e->FindClass("main/ConstructorObject"); // Update this to match your actual package path
    if (c == NULL) {
        e->ThrowNew(e->FindClass("java/lang/ClassNotFoundException"), "Could not find ConstructorObject class");
        return;
    }

    // 2. Get the constructor method ID
    jmethodID mid = e->GetMethodID(c, "<init>", "()V");
    if (mid == NULL) {
        e->ThrowNew(e->FindClass("java/lang/NoSuchMethodException"), "Could not find ConstructorObject() constructor");
        return;
    }

    // 3. Create the new object
    jobject newObj = e->NewObject(c, mid);
    if (newObj == NULL) {
        e->ThrowNew(e->FindClass("java/lang/InstantiationException"), "Failed to create ConstructorObject instance");
        return;
    }

    // Note: If you plan to use newObj beyond this function, you may need to create a global reference
    // e->NewGlobalRef(newObj);
}

Additional Checks & Tips

  • Fix the typo in your Java code: You have a typo in NativeNewObject—the method is named callConstrtuctorObject (extra 't' in Constructor). It's not causing the crash, but it's confusing and could lead to bugs later.
  • Check the hs_err_pid log: The log file mentioned in the crash message has detailed stack traces that can pinpoint exactly which line of your JNI code caused the crash. Always refer to this when debugging JVM crashes.
  • Classpath issues: If you're still having trouble after fixing the class name, double-check that your ConstructorObject class is properly compiled and present in the JVM's classpath when running the application.

内容的提问来源于stack exchange,提问作者Ghost

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:05:12