[LWJGL] Library.loadNative does not work, System.load does for custom lib

Good day everyone,

I have a small project which uses jni and till now I had the dynamic library in an fixed folder for testing.
Now I am trying to bundle it within the jar and because I am using Lwjgl anyway for convenience I wanted to use the loading facilities from org.lwjgl.system.Library.

This does not work for me I received an java.lang.UnsatisfiedLinkError: test.Test.add(II)I Exception.
Lwjgl3 provides the following log for a lib not packed (And a similar for one with a packed lib):

[LWJGL] Version: 3.1.6 build 14
[LWJGL] 	 OS: Windows 10 v10.0
[LWJGL] 	JRE: 10.0.1 amd64
[LWJGL] 	JVM: Java HotSpot(TM) 64-Bit Server VM v10.0.1+10 by "Oracle Corporation"
[LWJGL] Loading library (system): lwjgl
[LWJGL] 	Found at: C:\Users\CJG\AppData\Local\Temp\lwjglCJG\3.1.6-build-14\lwjgl.dll
[LWJGL] 	Loaded from org.lwjgl.librarypath: C:\Users\CJG\AppData\Local\Temp\lwjglCJG\3.1.6-build-14\lwjgl.dll
[LWJGL] Loading library: C:\Users\CJG\eclipse-workspace\test\build\lib\main\release\test.dll
[LWJGL] MemoryUtil accessor: MemoryAccessorUnsafe
[LWJGL] 	Success

When I use System#load or System#loadLibrary (Even when using an absolute path to the file lwjgl3 extracted) everything works as expected.

When I use System#load after calling Library#loadNative the Exception stays the same.

Do I have some misconception about Library? Anyway, thanks to everyone who can help me (or can not help me but took the time to consider if he possibly could :wink: )

Have a nice weekend
-ClaasJG

org.lwjgl.system.Library.loadNative() is not meant to load JNI libraries that the JVM can then resolve native methods from.
Instead, org.lwjgl.system.Library is meant to load the raw shared library using OS-specific API calls which the JVM does not know anything about in order to manually resolve function pointer addresses via SharedLibrary/FunctionProvider.getFunctionAddress().
In order to load a JNI library (and have the JVM know about that so that it can resolve native methods to JNI functions), you have to use System.load(), System.loadLibrary(), Runtime.load() or Runtime.loadLibrary().

Ok, thanks for the explanation :slight_smile:

-ClaasJG

The org.lwjgl.system.Library class supports two library loading methods:

  • [icode]SharedLibrary loadNative()[/icode], used for standard native libraries (e.g. GLFW or the OpenGL/Vulkan ICDs).
  • [icode]void loadSystem()[/icode], used for JNI libraries.

Both support the same mechanism for discovering shared libraries from paths (java.library.path / org.lwjgl.librarypath / system paths) or JAR files (automatically extracted to a temp or user-specified folder). Switching from loadNative() to loadSystem() should fix the issue.

Hui, I am to dumb to browse documentation it seems :persecutioncomplex:

It works like a charm now and replaces my hackish code.

-ClaasJG