Search code examples
javacjvmjava-native-interface

JVM exits without any error messages when loading compiled DLL


I'm trying to call a c function from Java. When loading the library (in Test.java) 2 things happen randomly:

  • "Load Lib" gets printed, and the jvm just exits without any errors
  • "Load Lib" gets printed, and the jvm gets stuck in a loop

The weird thing is that 'sometimes' "Lib loaded" gets printed too. Which means the library got loaded...

My question is that how can I fix this? The real problem is that I don't know what I'm doing wrong.

Dll compilation steps:

  • gcc -fpic -I "C:\Program Files\Java\jdk-15\include" -I "C:\Program Files\Java\jdk-15\include\win32" -c BindLib.c BindLib.h
  • gcc -fpic -s -shared -o BindLib.dll BindLib.o

System info:

  • Windows 10 64 bit, version 1909
  • Java 15

Main file:

package degubi;

public final class Main {

    public static void main(String[] args) {
        Test.enable();
    }
}

Library file:

package degubi;

public class Test {
    static {
        System.out.println("Load lib");
        System.loadLibrary("BindLib");
        System.out.println("Lib loaded");
    }

    public static native void enable();
}

Source file:

#include "windows.h"
#include "BindLib.h"

JNIEXPORT void JNICALL Java_degubi_Test_enable(JNIEnv* env, jclass clazz) {

}

Header file:

#define __int64 long long
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class degubi_Test */

#ifndef _Included_degubi_Test
#define _Included_degubi_Test
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     degubi_Test
 * Method:    enable
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_degubi_Test_enable(JNIEnv *, jclass);

#ifdef __cplusplus
}
#endif
#endif

Solution

  • I ended up creating a project in Visual Studio and building it from there... worked perfectly. Still don't know what caused the issue.