CMake not detecting JNI properly with Temuring jdk 8 in MacOS

946 Views Asked by At

I am building a library on Mac Big Sur using Java 8 from Adoptium (Eclipse Temurin).

To install Java, I am using the following commands:

brew tap homebrew/cask-versions
brew install --cask temurin8

Then, I export JAVA_HOME like this:

export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)

And it properly exports it. When I execute echo $JAVA_HOME, it returns:

/Library/Java/JavaVirtualMachines/temurin-8.jdk/Contents/Home

However, while building the library, it tries to find JNI, and it is returning the following lines:

CMake Error at /usr/local/Cellar/cmake/3.24.2/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
  Could NOT find JNI (missing: JAVA_INCLUDED_PATH JAVA_INCLUDED_PATH2 AWT)

Is there anything else necessary to make this work on MacOS?

Notes:

  • I installed cmake using brew (brew install cmake).
  • I have already searched in Google and StackOverflow, and none of the proposed solutions worked (I have also checked the recommended similar questions while creating this one).
  • I checked the variable values in the FindJNI.cmake file and everything seems correct (it should find it).
    • In FindJNI.cmake, the value of JAVA_AWT_INCLUDE_DIRECTORIES is correct (/Library/Java/JavaVirtualMachines/temurin-8.jdk/Contents/Home/include), and jni.h is inside that directory. However, find_path(JAVA_INCLUDE_PATH 'jni.h' $(JAVA_AWT_INCLUDE_DIRECTORIES)) is not setting JAVA_INCLUDE_PATH properly.
    • I created a dummy project using find_path(TEST 'jni.h' '/Library/Java/JavaVirtualMachines/temurin-8.jdk/Contents/Home/include'), looking for jni.h and it works. TEST contains the correct value.

Thank you.

Edit:

  • This is the call to find_package:

     find_package(JNI 1.7 REQUIRED)
    
1

There are 1 best solutions below

0
On BEST ANSWER

I was finally able to solve this by setting the requested variables as flags for cmake. I just appended the following to my cmake call:

cmake ... -DJAVA_HOME=$(/usr/libexec/java_home -v 1.8) -DJAVA_INCLUDE_PATH=$(/usr/libexec/java_home -v 1.8)/include -DJAVA_INCLUDE_PATH2=$(/usr/libexec/java_home -v 1.8)/include/darwin -DJAVA_AWT_INCLUDE_PATH=$(/usr/libexec/java_home -v 1.8)/include