How to fix EXCEPTION_ACCESS_VIOLATION when loading c++ dll file?

2

The Problem

I am writing a simple Hello World program using the Java Native Interface (JNI). When I call System.loadLibrary("libsimple_JNI") to load the dll file containing the C++ hello world function, an EXCEPTION_ACCESS_VIOLATION (0xc0000005) occurs (I am compiling in Netbeans with 64 bit cygwin).

I have tried statically loading the library as well as loading it within the Java helper function which calls the C++ hello world function. I added the path to the cygwin1.dll in the system environment variables which resolved earlier problems with Java not being able to find the dependencies for my dll file.

Here is the exact error message

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

EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x0000000180128dd7, pid=19116, tid=0x0000000000003ea8

 JRE version: Java(TM) SE Runtime Environment (8.0_131-b11) (build 1.8.0_131-b11)
 Java VM: Java HotSpot(TM) 64-Bit Server VM (25.131-b11 mixed mode windows-amd64 compressed oops)
 Problematic frame:
 C  [cygwin1.dll+0xe8dd7]

 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\Kogondo Admin\Desktop\JavaApplication7\hs_err_pid19116.log

Here is the Java Code

public static void main(String[] args) {
        thingy my_thingy = new thingy();
        my_thingy.hi();
}
public class thingy {
    public thingy() {}

    public void hi(){
        System.loadLibrary("libsimple_JNI");
        hello();
    }

    private native void hello();
}

Here is the C++ code

void JNICALL Java_javaapplication7_thingy_hello (JNIEnv * env, jobject object){

     printf("hi from C");

}

When running properly, I expect the code to print "hi from C" to the output window in Netbeans.

c++
netbeans
java-native-interface
cygwin
access-violation
asked on Stack Overflow May 9, 2019 by David • edited May 9, 2019 by YesThatIsMyName

1 Answer

2

Final Solution

7 and half hours I finally managed to find a solution. I followed van dench's advice to generate my dll file using the minGW compiler in Visual Studios instead of the cygwin compiler in Netbeans.

Here is a link to the video and the paper that I found which walk through how to do this. Note that the video is in Portuguese. That being said, the paper which it follows is in English and the video walkthrough shows step by step what commands and configurations you need to do.

I am willing to further elaborate on my solution if others would find that useful. Good Luck!

answered on Stack Overflow May 9, 2019 by David • edited May 9, 2019 by David

User contributions licensed under CC BY-SA 3.0