简体   繁体   English

在 JNI 中创建新对象

[英]Creating new objects in JNI

I've been creating some code in JNI and most of it is working fine but I haven't been able to create an instance of a Java object from C++ as my Java IDE gives me the following error:我一直在 JNI 中创建一些代码,其中大部分工作正常,但我无法从 C++ 创建 Java 对象的实例,因为我的 Java IDE 给了我以下错误:

A fatal error has been detected by the Java Runtime Environment: Java 运行时环境检测到一个致命错误:

EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x0000000070e1e0d2, pid=10100, tid=0x0000000000000d90 EXCEPTION_ACCESS_VIOLATION (0xc0000005) 在 pc=0x0000000070e1e0d2,pid=10100,tid=0x0000000000000d90

JRE version: Java(TM) SE Runtime Environment (8.0_101-b13) (build 1.8.0_101-b13) Java VM: Java HotSpot(TM) 64-Bit Server VM (25.101-b13 mixed mode windows-amd64 compressed oops) Problematic frame: V [jvm.dll+0x15e0d2] JRE 版本:Java(TM) SE 运行时环境 (8.0_101-b13) (build 1.8.0_101-b13) Java VM:Java HotSpot(TM) 64 位服务器 VM(25.101-b13 混合模式 windows-amd64 压缩 oops)有问题框架:V [jvm.dll+0x15e0d2]

Failed to write core dump.无法写入核心转储。 Minidumps are not enabled by default on client versions of Windows默认情况下,客户端版本的 Windows 上不启用小型转储

An error report file with more information is saved as: C:\\Users\\Alienware\\Documents\\Netbeans Projects\\NativeTest\\hs_err_pid10100.log包含更多信息的错误报告文件保存为:C:\\Users\\Alienware\\Documents\\Netbeans Projects\\NativeTest\\hs_err_pid10100.log

If you would like to submit a bug report, please visit: http://bugreport.java.com/bugreport/crash.jsp如果您想提交错误报告,请访问: http : //bugreport.java.com/bugreport/crash.jsp

The Java object I'm trying to create is:我试图创建的 Java 对象是:

public class ConstructorObject {    
    public ConstructorObject() {
        System.out.println("Hello World!");
    }
}

and the native class is:和本地类是:

public class NativeNewObject
{
    static {
        System.loadLibrary("NativeLibrary");
    }

    private native void callConstructorObject0();

    public void callConstrtuctorObject() {
        callConstructorObject0();
    }
}

The C++ code is: C++代码是:

JNIEXPORT void JNICALL Java_main_NativeNewObject_callConstructorObject0(JNIEnv *e, jobject obj) {
    jclass c = e->FindClass("ConstructorObject");
    jmethodID mid = e->GetMethodID(c, "<init>", "()V");
    jobject newObj = e->NewObject(c, mid);
}

Thanks.谢谢。

Was your class lookup successful?你的班级查询成功了吗? Did you see that the value of c contained something other than NULL?您是否看到c的值包含除 NULL 以外的其他内容?

Not sure if this helps, I had a problem on the line of constructor fetching, I had something like this:不确定这是否有帮助,我在构造函数获取方面遇到了问题,我有这样的事情:

jmethodID mid = e->GetMethodID(c, "<init>", "()V");

I changed it for我改变了它

jmethodID mid = e->GetMethodID(c, "<init>", "void(V)");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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