APIs use wrong classloader in Java

I have more than one class loader:

  • SystemClassloader
  • a logical classloader (using URLClassLoader)

Java on Windows, OpenCV version 4.5.2
the jar is in the classpath, the DLL in the path.

Loading the library succeeds
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
but when I’m trying to use the APIs the link is unsatisfied.

for instance
Mat findFace = Imgcodecs.imread("C:\\image.jpg");

after closer look I noticed that loadLibrary uses classloader of the caller class
Runtime.getRuntime().loadLibrary0(Reflection.getCallerClass(), libname);
but, the APIs are relying on SystemClassloader.

In debug I managed to make it working by evaluating the following statement
[00:28] Ivan Kostyuk
I did this in debug to prove it is working

ClassLoader.getSystemClassLoader().nativeLibraries.add(MyClass.class.getClassLoader().nativeLibraries.get(0))

This is ugly and cannot be implemented as a workaround, due to the field nativeLibraries has default visibility and it is really ugly.

I think it is an issue with OpenCV.
Could you recommend me the next step to get it resolved?

can you explain the need for a 2nd, non-standard classloader ?
how are you starting all this ?

where in your program are you calling opencv functions ?
are you sure, libraries are loaded at this time ?

which usually would be the class containing Main()

seriously, you must be doing something way off there ;(