How to fix Java 15 JNI "UnsatisfiedLinkError: A dynamic link library (DLL) initialization routine failed"

  c++, java, java-native-interface

I have found other posts of people having this exact error, but not one thus far has had a solution that worked for me. For reference, here are the things I have found:

https://community.oracle.com/tech/developers/discussion/2233828/jni-link-error-a-dynamic-link-library-dll-initialization-routine-failed

JNI UnsatisfiedLinkError: A dynamic link library (DLL) initialization routine failed

https://www.debugcn.com/en/article/5175409.html

https://coderanch.com/t/132356/engineering/java-lang-UnsatisfiedLinkError

Either their solution was not relevant to my particular scenario, or it did not fix the issue for me.

Everything is being compiled on the command line with a Windows 10 computer and using GCC (gcc-5.1.0-tdm64-1-c++) for compiling the C++ portions into a .dll, and JDK 15.0.1’s javac tool. There are three relevant files here, one being the header file derived from the java file.

Main.java:

public class Main {
    static {
        System.load("C:Users17659DocumentsProgrammingC++ & Java - JNI Testslibrary.dll");
        //System.loadLibrary("library");
    }
    public static void main(String[] args) {
        new Main().outputText();
    }
    
    private native void outputText();
}

Main.h:

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

#ifndef _Included_Main
#define _Included_Main
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     Main
 * Method:    outputText
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_Main_outputText
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

Library.cpp:

#include <iostream>
#include "Main.h"

JNIEXPORT void JNICALL Java_Main_outputText(JNIEnv * a, jobject b)
{
    std::cout << "testing";
}

They are contained all within the folder with the absolute path of C:Users659DocumentsProgrammingC++ & Java - JNI Tests. With a command prompt set to that as the current directory, I run the following commands in order:

g++ -c -o Library.o -I"C:Users659Documentsjdk-15.0.1include" -I"C:Users659Documentsjdk-15.0.1includewin32" Library.cpp
g++ -shared -o library.dll Library.o
javac Main.java
java Main

Despite the multiple things I have tried, I always get this same error:

Exception in thread "main" java.lang.UnsatisfiedLinkError: C:Users659DocumentsProgrammingC++ & Java - JNI Testslibrary.dll: A dynamic link library (DLL) initialization routine failed
        at java.base/jdk.internal.loader.NativeLibraries.load(Native Method)
        at java.base/jdk.internal.loader.NativeLibraries$NativeLibraryImpl.open(NativeLibraries.java:383)
        at java.base/jdk.internal.loader.NativeLibraries.loadLibrary(NativeLibraries.java:227)
        at java.base/jdk.internal.loader.NativeLibraries.loadLibrary(NativeLibraries.java:169)
        at java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2407)
        at java.base/java.lang.Runtime.load0(Runtime.java:747)
        at java.base/java.lang.System.load(System.java:1857)
        at Main.<clinit>(Main.java:3)

I have used nm on the resulting .dll to make sure the name of the function is correct, and it does seem to be exactly as it should.

The entire point of this little project of mine is to figure out how JNI works, since I have a plan to write a small portion of a program in C++. The rest of the program though would work best in Java (for me). I do not know what I need to do to get this program to work, I have spent approximately 2 hours of googling and fiddling attempting to get it to function. This is on a 64-bit OS. How can I make this program run and print out the very little amount of text I would like it to print out?

Source: Windows Questions C++

LEAVE A COMMENT