Link issues for opencv with java/kotlin

repo

I’ve tried linking to opencv using two unoffical maven packages then I recently just add the opencv-455.jar to a directory to forcefully include but I’m getting runtime linking issues

I’m able to successfully build but running gives me

Exception in thread "main" java.lang.UnsatisfiedLinkError: 'long org.opencv.core.Mat.n_Mat()'
        at org.opencv.core.Mat.n_Mat(Native Method)
        at org.opencv.core.Mat.<init>(Mat.java:23)
        at org.opencv.core.MatOfInt.<init>(MatOfInt.java:35)
        at frc.vision.MainKt.main(Main.kt:10)
        at frc.vision.MainKt.main(Main.kt)

My main function looks like

import org.opencv.core.*
import org.opencv.videoio.*

fun main() = runBlocking {
    println("Attempting a connection to the robot")
    val client = RobotClient()

    val ball_cam_props = MatOfInt(
        Videoio.CAP_PROP_FRAME_WIDTH, 480,
        Videoio.CAP_PROP_FRAME_HEIGHT, 640
    )
...
}

Am I doing something wrong in my code or is by build.gradle.kts wrong?

ouch ! and even 2 of them …

ouuuucccchhhh ! this really hurts.
stick with a single version, remove all others. common sense, no ?

also, the jar only contains stubs / java wrappers. opencv is a c++ library, you need the corresponding opencv_java.dll/so, containing the real implementation code.
(and that’s what is missing here)

you also need to load that as the 1st thing in main() (or in a staticblock) similar to:

static {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}

also, kotlin might behave somewhat special here, last time i tried (3 years ago), i had to jump through some hoops like:


fun ocv_init() {
	System.setProperty("java.library.path", "c:/p/kotlinc/bin31/Release/") //path to dll
	System.setProperty("java.awt.headless", "false") // why on earth ?
	//System.loadLibrary(Core.NATIVE_LIBRARY_NAME) // nope ;( !!!!!
	val lp = System.getProperty("java.library.path")
	System.load(lp + Core.NATIVE_LIBRARY_NAME + ".dll")
}

please have a look at the tutorials (though outdated)
and good luck with it !

Should have specified, I wasn’t trying to link to multiple versions at the same time. When I’m trying to load opencv do I put that static block in Main.kt or do I have to put it in gradle?

so

static {
    System.loadLibraries(Core.OpenCV)
}

?

yea, main.kt.
1st line in main() should do, too.
(and no idea about gradle, maven or kotlin, sorry)

I added System.loadLibrary(System.getProperty("java.library.path") + Core.NATIVE_LIBRARY_NAME + ".so") and I’m now getting

Exception in thread "main" java.lang.UnsatisfiedLinkError: Directory separator should not appear in library name: /usr/java/packages/lib:/usr/lib64:/lib64:/lib:/usr/libopencv_java455.so
	at java.base/java.lang.Runtime.loadLibrary0(Runtime.java:827)
	at java.base/java.lang.System.loadLibrary(System.java:1873)
	at frc.vision.MainKt.main(Main.kt:7)
	at frc.vision.MainKt.main(Main.kt)

lookup the difference between System.load() and System.loadLibrary()

I tried setting like this

    System.setProperty("java.library.path", "/usr/local/share/java/opencv4/")
    System.load(System.getProperty("java.library.path") + Core.NATIVE_LIBRARY_NAME + ".so")

Even tried with a version of opencv compiled on my system and using the jar locally.
Exception in thread "main" java.lang.UnsatisfiedLinkError: Can't load library: /usr/local/share/java/opencv4/opencv_java455.so

yea, i know. i found that part entirely frustrating, you got all my sympathy.
(unfortunately, i’m no more doing any java/kotlin dev, and i can help no more, sorry)