'Incompatible architecture problem using OpenCV/Java in Eclipse on Apple M1

I have a MacBookPro with the new M1 chip. My plan is to use it as development platform for opencv with Java. DevEnv is Eclipse.

In order to install opencv I followed this instructions:

  1. brew install ant
  2. brew edit opencv I change the line: -DBUILD_opencv_java=OFF in -DBUILD_opencv_java=ON to have Java support

then

  1. brew install --build-from-source opencv

(I have installed Xcode before for other project and also the command line tools.)

The compilation was successful!

Then I setup a simple project adding in the build path an external library which points to the compiled jar. I also add the path to the corresponding dylib.

Eclipse find the libraries!

This is the java code for the program:

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;

public class OpenCVTest {

    public static void main(String[] args) {
        System.out.println("hello world");
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        Mat mat = Mat.eye(3, 3, CvType.CV_8UC1);
        System.out.println("mat = " + mat.dump());
    }

}

No compilation errors marked in the editor.

BUT when I start the program, it complains with this error (console):

hello world Exception in thread "main" java.lang.UnsatisfiedLinkError: /opt/homebrew/Cellar/opencv/4.5.4_4/share/java/opencv4/libopencv_java454.dylib: dlopen(/opt/homebrew/Cellar/opencv/4.5.4_4/share/java/opencv4/libopencv_java454.dylib, 0x0001): tried: '/opt/homebrew/Cellar/opencv/4.5.4_4/share/java/opencv4/libopencv_java454.dylib' (mach-o file, but is an incompatible architecture (have 'arm64', need 'x86_64')), '/usr/lib/libopencv_java454.dylib' (no such file) at java.base/jdk.internal.loader.NativeLibraries.load(Native Method) at java.base/jdk.internal.loader.NativeLibraries$NativeLibraryImpl.open(NativeLibraries.java:384) at java.base/jdk.internal.loader.NativeLibraries.loadLibrary(NativeLibraries.java:228) at java.base/jdk.internal.loader.NativeLibraries.loadLibrary(NativeLibraries.java:170) at java.base/jdk.internal.loader.NativeLibraries.findFromPaths(NativeLibraries.java:311) at java.base/jdk.internal.loader.NativeLibraries.loadLibrary(NativeLibraries.java:283) at java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2422) at java.base/java.lang.Runtime.loadLibrary0(Runtime.java:818) at java.base/java.lang.System.loadLibrary(System.java:1989) at OpenCVTest.main(OpenCVTest.java:9)

I checked in the console with file-command and it confirms that it is arm64, which I assume is the correct architecture.

I checked my Eclipse and that was X86. so I installed another eclipse (aarch64) with the right archtiecture.

Same error!

So what is the problem?

Thanks for the help.



Solution 1:[1]

Stupid me! I used aarch64-eclipse, aarch64-opencv BUT a X86_64 based Java under the hood. Thats why it demands X86 dylibs.

I installed a corretto18 with aarch64 now everything works for me, too.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Huebi