Creating new objects in JNI

-1

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:

A fatal error has been detected by the Java Runtime Environment:

EXCEPTION_ACCESS_VIOLATION (0xc0000005) at 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]

Failed to write core dump. Minidumps are not enabled by default on client versions of Windows

An error report file with more information is saved as: 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

The Java object I'm trying to create is:

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:

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.

java
c++
java-native-interface
asked on Stack Overflow Jan 3, 2018 by Ghost • edited Jan 3, 2018 by Michael

2 Answers

3

Was your class lookup successful? Did you see that the value of c contained something other than NULL?

answered on Stack Overflow Jan 3, 2018 by Edwin Buck
0

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

Now the error is one line b

answered on Stack Overflow Feb 1, 2020 by Levant Alejandro • edited Feb 1, 2020 by Simas Joneliunas

User contributions licensed under CC BY-SA 3.0