Build OpenCV 4.9.0 from source on Ubuntu with BUILD_opencv_java=ON

When running cmake like so:

cmake -D CMAKE_BUILD_TYPE=Release \
      -D CMAKE_INSTALL_PREFIX=/usr/local \
      -D BUILD_opencv_java=ON ..

Then Java is always NO.

--   Java:
--     ant:                         /bin/ant (ver 1.10.14)
--     Java:                        NO
--     JNI:                         /usr/lib/jvm/default-java/include /usr/lib/jvm/default-java/include/linux /usr/lib/jvm/default-java/include
--     Java wrappers:               YES (ANT)
--     Java tests:                  YES
--
--   Install to:                    /usr/local

And thus when building OpenCV, the file libopencv_java490.so is never added to ls /usr/local/lib.

I made sure to set the same Java and Javac versions:

export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
echo 'export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64' >> ~/.bashrc
source ~/.bashrc

Set the same Java and Javac versions like so:
sudo update-alternatives --config java

Set the full paths in cmake:

cmake -D CMAKE_BUILD_TYPE=Release \
      -D CMAKE_INSTALL_PREFIX=/usr/local \
      -D BUILD_opencv_java=ON \
      -D JAVA_AWT_LIBRARY=/usr/lib/jvm/java-11-openjdk-amd64/lib/libawt.so \
      -D JAVA_JVM_LIBRARY=/usr/lib/jvm/java-11-openjdk-amd64/lib/server/libjvm.so \
      -D JAVA_INCLUDE_PATH=/usr/lib/jvm/java-11-openjdk-amd64/include \
      -D JAVA_INCLUDE_PATH2=/usr/lib/jvm/java-11-openjdk-amd64/include/linux \
      -D JAVA_AWT_INCLUDE_PATH=/usr/lib/jvm/java-11-openjdk-amd64/include \

The cmake output always shows NO for Java. I tried this on fresh minimal Ubuntu 24 installs while making sure that all packages necessary packages are installed and up to date. And I tried with Linux subsystem on Windows 10. If you have any ideas to getting the Java part build, too, please let me know.

i could only look at cmake, but the status line here:

shows, that Java_FOUND is false:

so, looks like your cmake did not find a valid sdk
(like openjdk is not accepted)

If I’m not mistaken the source for find_package is here: https://github.com/Kitware/CMake/blob/master/Source/cmFindPackageCommand.cxx

But I wasn’t able to figure out what it does to find the package, or in what folders it looks for Java, jdk or jre.

Ramblings

Also I think if I install Java from Oracle I will have to pay a license fee. But that’s besides the point anyways. Also when Ubuntu offers commands to install Java, it only shows apt install commands with OpenJDK. From version 8 to 21. I very much believe that OpenJDK can be used to get a build with the file libopencv_java490.

If anyone was able to build OpenCV 4.9 using OpenJDK 8, 11 or later on Linux, please be so kind and share your commands.

looking at docs, i think, it’s this (cmake builtin):

Thanks for digging into the source files. When I run this command update-alternatives --list java on my Linux boxes running Ubuntu 20 and 24 I get resuluts like: /usr/lib/jvm/java-21-openjdk-amd64/bin/java or /usr/lib/jvm/java-11-openjdk-amd64/bin/java

These paths are not in FindJava.cmake:

set(_JAVA_PATHS
  /usr/lib/java/bin
  /usr/share/java/bin
  /usr/local/java/bin
  /usr/local/java/share/bin
  /usr/java/j2sdk1.4.2_04
  /usr/lib/j2sdk1.4-sun/bin
  /usr/java/j2sdk1.4.2_09/bin
  /usr/lib/j2sdk1.5-sun/bin
  /opt/sun-jdk-1.5.0.04/bin
  /usr/local/jdk-1.7.0/bin
  /usr/local/jdk-1.6.0/bin
  )

But as I understand one can set their own Java paths. And I tried doing this as I showed in my initial post

cmake -D CMAKE_BUILD_TYPE=Release \
      -D CMAKE_INSTALL_PREFIX=/usr/local \
      -D BUILD_opencv_java=ON \
      -D JAVA_AWT_LIBRARY=/usr/lib/jvm/java-11-openjdk-amd64/lib/libawt.so \
      -D JAVA_JVM_LIBRARY=/usr/lib/jvm/java-11-openjdk-amd64/lib/server/libjvm.so \
      -D JAVA_INCLUDE_PATH=/usr/lib/jvm/java-11-openjdk-amd64/include \
      -D JAVA_INCLUDE_PATH2=/usr/lib/jvm/java-11-openjdk-amd64/include/linux \
      -D JAVA_AWT_INCLUDE_PATH=/usr/lib/jvm/java-11-openjdk-amd64/include \

I just checked and I was able to verify the paths for all files. So for libawt.so, libjvm, and also the include folder. It exists in Java 11, no longer in Java 21. But since I was running the cmake command with all the paths on a ubuntu box with Java 11 installed and set as the default Java installation, it should have been alright. Or at least as far as I can tell. My knowledge is minimal on how to use cmake or how to build OpenCV.

mine, too _;(
however, i’d argue, that you have to convince cmake somehow to accept your (custom) openjdk install
(it’s not really an OpenCV specific problem)

did you check, if it returns the correct $VERSION string ?
maybe it needs to set JAVA_HOME or similar ?

not sure, if those vars you set are actually used to find your sdk