OpenCV build from source fails with Java 11: warning: target release 11 conflicts with default source release 18

Viewed 271

I’m working on a Java application that runs Java 11. I’ve chosen Java 11 because it’s the the last Java version with LTS. I’m using OpenCV in this Java application and am using libraries from the contrib modules. I’m using Homebrew to install OpenCV with the Java bindings and the contrib modules.

The first thing I tried was to install OpenCV from source without specifying the Java target version. When I did this, OpenCV installed correctly, but when I tried to use it in code, I got the following error:

class file has wrong version 61.0, should be 55.0

I take this to mean that because OpenCV was compiled via Java 17, I can’t use it in my Java 11 code. It’s also worth mentioning that because I’m using Maven with my Java project, I first export the OpenCV JAR into a local Maven module via something like mvn install:install-file -Dfile=/usr/local/Cellar/opencv/4.5.5_1/share/java/opencv4/opencv-455.jar. I’m using OpenJDK 11 as my JRE for this export.

I’m also using OpenJDK 11 as my default JDK on my Mac (java -version and javac -version both return OpenJDK 11). So I was a bit surprised to see this error. So to solve it, I’ve been trying to install OpenCV with the flag -DOPENCV_JAVA_TARGET_VERSION=11. However, when I do this, I get the following error:

[ 11%] Generating opencv-455.jar
cd /tmp/opencv-20220410-81173-p3o3uo/opencv-4.5.5/build/modules/java/jar/opencv && /usr/local/bin/ant -noinput -k jar
Picked up _JAVA_OPTIONS: -Duser.home=/Users/dpapp/Library/Caches/Homebrew/java_cache
Buildfile: /private/tmp/opencv-20220410-81173-p3o3uo/opencv-4.5.5/build/modules/java/jar/opencv/build.xml
   [tstamp] Honouring environment variable SOURCE_DATE_EPOCH which has been set to 1640404407

jar:
    [javac] Compiling 280 source files to /private/tmp/opencv-20220410-81173-p3o3uo/opencv-4.5.5/build/modules/java/jar/opencv/build/classes
    [javac] warning: target release 11 conflicts with default source release 18
Target 'jar' failed with message 'Compile failed; see the compiler error output for details.'.

BUILD FAILED
/private/tmp/opencv-20220410-81173-p3o3uo/opencv-4.5.5/build/modules/java/jar/opencv/build.xml:14: Compile failed; see the compiler error output for details

This error makes it sound like OpenCV requires at least JDK 18, which doesn’t sound right to me. Is there a minimum JDK for each OpenCV version? Should I be using OpenCV 3 instead? Any help is greatly appreciated, thank you!

1 Answers

I partially figured out what's going on here.

As a prereq for running brew install opencv, I ran brew install ant. brew install ant installs the latest OpenJDK (OpenJDK 18 at the time of writing) and ignores the Java home path set on my machine. This causes brew install opencv to try to install OpenCV with Java 18 while the target is set to 11, which throws an error.

Related