0

When you use scala repl simple call System.loadLibrary("opencv_410") (in case you are trying to load libopencv_410.so) does not make you able to use native library. If you try to create some object of the class with JNI calls it will tell you --- java.lang.UnsatisfiedLinkError, as if no library was loaded.

Welcome to Scala 2.12.8 (OpenJDK 64-Bit Server VM, Java 1.8.0_201).
Type in expressions for evaluation. Or try :help.

scala> System.loadLibrary(org.opencv.core.Core.NATIVE_LIBRARY_NAME)

scala> new org.opencv.core.Mat()
java.lang.UnsatisfiedLinkError: org.opencv.core.Mat.n_Mat()J
  at org.opencv.core.Mat.n_Mat(Native Method)
  at org.opencv.core.Mat.<init>(Mat.java:26)
  ... 24 elided

scala> 

Solution with no explanation is provided.

Vladimir Protsenko
  • 1,046
  • 2
  • 14
  • 21

1 Answers1

0

To load native library you should load it for the class scala.tools.nsc.interpreter.IMain. As two argument method loadLibrary0 of Runtime class is not accessible from our scope we use reflection to invoke it.

Welcome to Scala 2.12.8 (OpenJDK 64-Bit Server VM, Java 1.8.0_201).
Type in expressions for evaluation. Or try :help.

scala> val loadLibrary0 = Runtime.getRuntime.getClass.getDeclaredMethods()(4)
loadLibrary0.setAccessible(true)
loadLibrary0.invoke(Runtime.getRuntime, scala.tools.nsc.interpreter.ILoop.getClass, "opencv_java410")
loadLibrary0: java.lang.reflect.Method = synchronized void java.lang.Runtime.loadLibrary0(java.lang.Class,java.lang.String)

scala> 
scala> res1: Object = null

scala> new org.opencv.core.Mat()
res2: org.opencv.core.Mat = Mat [ 0*0*CV_8UC1, isCont=false, isSubmat=false, nativeObj=0x7f5162f2a1f0, dataAddr=0x0 ]
Vladimir Protsenko
  • 1,046
  • 2
  • 14
  • 21