I have followed countless instructions available online to install and run opencv Java on macOS M1 arm64 architecture but not been successful.
After lot of hit and trials i did following:
brew install opencv
then
brew edit opencv //to do -DBUILD_opencv_java=ON
then copied the entire opencv.rb to a new file at another location. Followed by
brew uninstall opencv
brew install /path/to/new/opencv.rb --build-from-source -v
which installs opencv correctly as I can verify with brew info opencv
which results in
brew info opencv
==> opencv: stable 4.9.0 (bottled)
Open source computer vision library
Home
Installed
/usr/local/Cellar/opencv/4.9.0_12 (974 files, 256.8MB) *
Built from source on 2024-07-04 at 10:50:47
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/o/opencv.rb
License: Apache-2.0
==> Dependencies
Build: cmake ✔, pkg-config ✔, python-setuptools ✔
Required: abseil ✔, ceres-solver ✔, eigen ✔, ffmpeg@6 ✔, freetype ✔, gflags ✔, glog ✔, harfbuzz ✔, jpeg-turbo ✔, libpng ✔, libtiff ✔, numpy ✔, openblas ✔, openexr ✔, openjpeg ✔, openvino ✔, protobuf ✔, [email protected] ✔, tbb ✔, tesseract ✔, vtk ✔, webp ✔, glew ✔, imath ✔, jsoncpp ✔, libarchive ✔
==> Analytics
install: 14,695 (30 days), 39,165 (90 days), 142,687 (365 days)
install-on-request: 13,918 (30 days), 37,238 (90 days), 133,670 (365 days)
build-error: 17 (30 days)
So I try to create a Java project, copy the two below from /usr/local/Cellar/opencv/4.9.0_12/share/java/opencv4
libopencv_java490.dylib
opencv-490.jar
into project directory and add jars and Library. Also set VM for -Djava.library.path=/usr/local/Cellar/opencv/4.9.0_12/share/java/opencv4
. But no matter what I do I always end up getting this error,
java: cannot access org.opencv.core.Core
bad class file: /path/to/src/src/opencv-490.jar!/org/opencv/core/Core.class
class file has wrong version 66.0, should be 61.0
Please remove or make sure it appears in the correct subdirectory of the classpath.
My system is out and out Java17, i.e version & Project structure.
java --version
java 17.0.9 2023-10-17 LTS
Java(TM) SE Runtime Environment (build 17.0.9+11-LTS-201)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.9+11-LTS-201, mixed mode, sharing)
What’s going wrong here?
I upgraded to Java 22 as suggested, but no result.
java --version
java 22.0.1 2024-04-16
Java(TM) SE Runtime Environment (build 22.0.1+8-16)
Java HotSpot(TM) 64-Bit Server VM (build 22.0.1+8-16, mixed mode, sharing)
Now the error is (which I had earlier too )
Exception in thread "main" java.lang.UnsatisfiedLinkError: /Path `to src/src/libopencv_java490.dylib: dlopen(/Path to src/src/libopencv_java490.dylib, 0x0001): tried: '/Path to src/src/libopencv_java490.dylib' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e' or 'arm64')), '/System/Volumes/Preboot/Cryptexes/OS/Users/vineetmishra//Path to src/src/libopencv_java490.dylib' (no such file), '/Path to src/src/libopencv_java490.dylib' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e' or 'arm64'))`
So either its version issue or Unsatisfiedlink error. In few trials I even did -D CMAKE_OSX_ARCHITECTURES=arm64
where cmake was being used, still no effect.
2