26

I'm trying to write a build for my project where I'm trying to replace autobuild, and I need to proper use FIND_JNI.

I could make a simple build but it's not properly finding jni.h

And I need to find a proper way (without a hack) to define the Java include as this needs to be portable to other users.

Clebert Suconic
  • 5,353
  • 2
  • 22
  • 35

2 Answers2

36

The following code works for me. In your root CMakeLists.txt file add:

find_package(JNI)

if (JNI_FOUND)
    message (STATUS "JNI_INCLUDE_DIRS=${JNI_INCLUDE_DIRS}")
    message (STATUS "JNI_LIBRARIES=${JNI_LIBRARIES}")
endif()
sakra
  • 62,199
  • 16
  • 168
  • 151
3

That solution did not worked for me, I used:

find_package(JNI REQUIRED)    
include_directories(${JNI_INCLUDE_DIRS})

and

echo $JAVA_HOME

must return a valid path.

Akrax
  • 53
  • 5
  • 1
    Note: if using CMake 3.24+, use the `JNI::JNI` target imported by `find_package(JNI REQUIRED)` (instead of old-school `*_INCLUDE_DIRS`): https://cmake.org/cmake/help/latest/module/FindJNI.html – JonasVautherin Dec 28 '22 at 12:50
  • @JonasVautherin could you please show a full example of how to use the new version? – Jire Jan 31 '23 at 07:38
  • Well something like `target_link_libraries(my_target PRIVATE JNI::JNI)` – JonasVautherin Jan 31 '23 at 09:04