2

I am trying to install OpenCV using the Homebrew-based installation instructions from the documentation.

brew edit opencv # edit file and set -DBUILD_opencv_java=ON 
brew install --build-from-source opencv

Then I try to use the resulting OpenCV jar in my Java project but it fails at runtime because the Java version used to compile does not match my runtime.

Caused by: java.lang.UnsupportedClassVersionError: org/opencv/core/Core has been compiled by a more recent version of the Java Runtime (class file version 54.0), this version of the Java Runtime only recognizes class file versions up to 52.0

I need it to be compiled with Java 8 but it is being compiled with Java 10.

How can I tell Homebrew/OpenCV which version of javac to use?

I have tried setting my JAVA_HOME to the desired location and it still does not work.

driangle
  • 11,601
  • 5
  • 47
  • 54
  • Where are you setting JAVA_HOME, and are you running `java -version` before building OpenCV? – OneCricketeer Nov 14 '18 at 04:14
  • See https://stackoverflow.com/questions/26252591/mac-os-x-and-multiple-java-versions – Ori Marko Nov 14 '18 at 04:20
  • I've tried setting JAVA_HOME in my `~/.bash_profile` and adding it to my `PATH`. I have also tried using `jenv` and setting the global java version. Yes, before building OpenCV (using command above) I've run `java -version` and it returns: `java version "1.8.0_192"` – driangle Nov 14 '18 at 04:36
  • I am having similar issue. Anybody managed to solve this? – Yoku May 02 '20 at 21:01

1 Answers1

3

See Set target java version when build OpenCV with brew

Where user minhtus answers his own question;

Found the answer, put the extra -DOPENCV_JAVA_TARGET_VERSION=1.8 args to cmake in brew formula.

You can do this using the command;

brew edit opencv

And then in the editor, look for;

args = std_cmake_args + %W[
  -DCMAKE_OSX_DEPLOYMENT_TARGET=
  -DBUILD_JASPER=OFF
  -DBUILD_JPEG=OFF
  -DBUILD_OPENEXR=OFF
  -DBUILD_PERF_TESTS=OFF

And insert the the java version flag.

SeanOwens
  • 31
  • 2