# find\_package(JNI) not able to find JNI (cmake v 3.17)

**URL:** https://discourse.cmake.org/t/find-package-jni-not-able-to-find-jni-cmake-v-3-17/2075
**Category:** Usage
**Tags:** os:linux
**Created:** [October 27, 2020, 5:47pm UTC](https://discourse.cmake.org/t/find-package-jni-not-able-to-find-jni-cmake-v-3-17/2075 "2020-10-27T17:47:22Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![yisseamake](https://discourse.cmake.org/user_avatar/discourse.cmake.org/yisseamake/32/897_2.png) [@yisseamake](https://discourse.cmake.org/u/yisseamake)
#### Post date: [October 27, 2020, 5:47pm UTC](https://discourse.cmake.org/t/find-package-jni-not-able-to-find-jni-cmake-v-3-17/2075/1 "2020-10-27T17:47:22Z")

</div>

I am getting error indicating failure to find JNI package while it is installed correctly in pretty standard location /opt/jdk/jdk8 (ubuntu 14.04, e.g /opt/jdk/jdk8/jre/lib/amd64/server/libjvm.so ) and JAVA\_HOME variable is set appropriately before the call to find\_package(JNI). the find\_package(Java) called prior to find\_package(JNI) successfully finds Java. The debug log for find\_package(JNI) shows that instead of considering path set by JAVA\_HOME it only considers /opt/myp, /opt/toolchains. This problems goes away after setting JAVA\_AWT\_LIBRARY, JAVA\_JVM\_LIBRARY, JAVA\_INCLUDE\_PATH, JAVA\_INCLUDE\_PATH2, JAVA\_AWT\_INCLUDE\_PATH prior to calling find\_package(JNI)  
But such setting defeats the purpose of find\_package(JNI) so I wonder if someone could suggest the fix.  
Also the [cmake.org](http://cmake.org) description of FindJNI is too vague : “The caller may set variable `JAVA_HOME` to specify a Java installation prefix explicitly.”. This is unlike FindJAVA where the setting JAVA\_HOME is specified. So what setting is needed to hint the location?

> **Sample CMake project**
>
> ```cmake
> cmake_minimum_required(VERSION 3.17)
> include(UseJava)
> if(DEFINED ENV{JAVA_HOME})
> set(JAVA_HOME "$ENV{JAVA_HOME}")
> else()
> set(JAVA_HOME /opt/jdk/jdk8)
> endif()
> message(STATUS "JAVA_HOME variable is defined or set as '${JAVA_HOME}'")
> 
> # set(JAVA_AWT_LIBRARY "${JAVA_HOME}/lib/amd64") #the path to the Java AWT Native Interface (JAWT) library
> # set(JAVA_JVM_LIBRARY "${JAVA_HOME}/lib/amd64/server") #the path to the Java Virtual Machine (JVM) library
> # set(JAVA_INCLUDE_PATH "${JAVA_HOME}/include") #the include path to jni.h
> # set(JAVA_INCLUDE_PATH2 "${JAVA_HOME}/include/linux") # the include path to jni_md.h and jniport.h
> # set(JAVA_AWT_INCLUDE_PATH "${JAVA_HOME}/include") # the include path to jawt.h
> # set(CMAKE_FIND_ROOT_PATH "${JAVA_HOME}")
> # set(CMAKE_FIND_USE_CMAKE_SYSTEM_PATH "${JAVA_HOME}")
>    
> set(CMAKE_FIND_DEBUG_MODE TRUE)
> find_package(JNI ) #TODO@ add REQUIRED, But cant use PATHS ${JAVA_HOME} as it triggers FindProjname.cmake mode
> set(CMAKE_FIND_DEBUG_MODE FALSE)
> 
> if(JNI_FOUND)
> message (STATUS " JNI_INCLUDE_DIRS=${JNI_INCLUDE_DIRS}")
> message (STATUS " JNI_LIBRARIES=${JNI_LIBRARIES}")
> else()
> message (STATUS " JNI is NOT FOUND")
> endif()
> 
> ```

> **Sample output**
>
> ```auto
> -- Found Java: /opt/jdk/jdk8/bin/java (found version "1.8.0.161") found components: Development 
> 
> -- JAVA_HOME variable is defined or set as '/opt/jdk/jdk8'
> CMake Debug Log at /opt/cmake/share/cmake-3.17/Modules/FindJNI.cmake:321 (find_library):
> find_library(JAVA_JVM_LIBRARY) removed original suffix
> /opt/toolchains/gcc-linaro-arm-linux-gnueabihf-4.8-2014.04_linux/home/cnh/build-scripts/
> from PATH_SUFFIXES while adding architecture paths for suffix '32'
> Call Stack (most recent call first):
> ...myproj/src/CMakeLists.txt:30 (find_package)
> 
> CMake Debug Log at /opt/cmake/share/cmake-3.17/Modules/FindJNI.cmake:321 (find_library):
> find_library(JAVA_JVM_LIBRARY) added replacement path
> /opt/toolchains/gcc-linaro-arm-linux-gnueabihf-4.8-2014.04_linux/bin/ to
> PATH_SUFFIXES for architecture suffix '32'
> Call Stack (most recent call first):
> myproj/src/CMakeLists.txt:30 (find_package)
> ...
> find_library called with the following settings:
> VAR: JAVA_JVM_LIBRARY
> NAMES: "jvm"
> Documentation: Path to a library.
> Framework
> Only Search Frameworks: 0
> Search Frameworks Last: 0
> Search Frameworks First: 0
> AppBundle
> Only Search AppBundle: 0
> Search AppBundle Last: 0
> Search AppBundle First: 0
> CMAKE_FIND_USE_CMAKE_PATH: 1
> CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH: 1
> CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH: 1
> CMAKE_FIND_USE_CMAKE_SYSTEM_PATH: 1
> 
> find_library considered the following locations:
> 
> /opt/toolchains/gcc-linaro-arm-linux-gnueabihf-4.8-2014.04_linux/bin/(lib)jvm(\.so|\.a)
> /opt/toolchains/gcc-linaro-arm-linux-gnueabihf-4.8-2014.04_linux/bin/(lib)jvm(\.so|\.a)
> /opt/toolchains/gcc-linaro-arm-linux-gnueabihf-4.8-2014.04_linux/lib/(lib)jvm(\.so|\.a)
> /opt/myp/3.7.0/sysroots/cortexa9hf-vfp-neon-myp-linux-gnueabi/usr/sbin/(lib)jvm(\.so|\.a)
> /opt/myp/3.7.0/sysroots/cortexa9hf-vfp-neon-myp-linux-gnueabi/usr/bin/(lib)jvm(\.so|\.a)
> /opt/myp/3.7.0/sysroots/cortexa9hf-vfp-neon-myp-linux-gnueabi/sbin/(lib)jvm(\.so|\.a)
> /opt/myp/3.7.0/sysroots/cortexa9hf-vfp-neon-myp-linux-gnueabi/bin/(lib)jvm(\.so|\.a)find
> /opt/myp/3.7.0/sysroots/cortexa9hf-vfp-neon-myp-linux-gnueabi/usr/local/(lib)jvm(\.so|\.a)
> /opt/myp/3.7.0/sysroots/cortexa9hf-vfp-neon-myp-linux-gnueabi/usr/lib/(lib)jvm(\.so|\.a)
> /opt/myp/3.7.0/sysroots/cortexa9hf-vfp-neon-myp-linux-gnueabi/usr/(lib)jvm(\.so|\.a)
> /opt/myp/3.7.0/sysroots/cortexa9hf-vfp-neon-myp-linux-gnueabi/lib/(lib)jvm(\.so|\.a)
> /opt/myp/3.7.0/sysroots/cortexa9hf-vfp-neon-myp-linux-gnueabi/opt/(lib)jvm(\.so|\.a)
> 
> The item was not found.
> ...and so on for other variables that supposed to be found...
> 
> ```

Note: initially this issue was reported as a part of

> [@GENERATE\_NATIVE\_HEADERS target is not recognized as INTERFACE type](https://discourse.cmake.org/t/generate-native-headers-target-is-not-recognized-as-interface-type/2048/7):
>
> If I drop myprojJNI from the list of targets in the install(TARGETS, then error occurs (below ) indicating that dependendee target myprojlibShared needs its dependency myprojJNI because they are linked by target\_link\_libraries(myprojlibShared …myprojJNI …) install(TARGETS myprojlibShared myprojJNI … #need myprojJNI bc it was linked EXPORT myExportTargets … ) ERROR: CMake Error: install(EXPORT “myExportTargets” …) includes target “myprojlibShared” which requires target “myprojJNI” that i…

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [November 7, 2020, 1:25am UTC](https://discourse.cmake.org/t/find-package-jni-not-able-to-find-jni-cmake-v-3-17/2075/2 "2020-11-07T01:25:04Z")

</div>

Anyone more familiar with using Java/JNI in CMake projects able to offer any insights here? I can’t tell if this is a bug or perhaps just a doc issue.

---

<div class="post-metadata">

### Author: ![yisseamake](https://discourse.cmake.org/user_avatar/discourse.cmake.org/yisseamake/32/897_2.png) [@yisseamake](https://discourse.cmake.org/u/yisseamake)
#### Post date: [November 9, 2020, 5:09pm UTC](https://discourse.cmake.org/t/find-package-jni-not-able-to-find-jni-cmake-v-3-17/2075/3 "2020-11-09T17:09:11Z")

</div>

It appears that behavior is influenced by setting CMAKE\_TOOLCHAIN\_FILE option in cmake (e.g. -DCMAKE\_TOOLCHAIN\_FILE=$ROOT/…/android.toolchain.cmake). However this file has not mentioning of any Java related variables, just C/C++/… options. It does set \_CMAKE\_TOOLCHAIN\_PREFIX. Therefore I tried to set CMAKE\_PREFIX\_PATH (and Java\_DIR ) to ${JAVA\_HOME} prior to calling find\_package. So it must be some other variable that force search off JAVA\_HOME.  
I had to move find\_package(Java) to be before project() to ensure Java is Found ok. This is because cmake prefixes search path for Java by a toolchain path even though toolchain has nothing to do with Java (r. pnly C++/C/ASM). When find\_package(Java) moved up before project() statement , cmake finds java correctly bc it sets CMAKE\_PREFIX\_PATH only after project(). Ensuring JAVA\_HOME settings and find\_package(Java) success is still not sufficient to make find\_package(JNI) working without the kludge described above.  
find\_package(JNI) continues to prepend JNI file search path with a path specified by the toolchain path when -DCMAKE\_TOOLCHAIN\_FILE set. Attempts to reset CMAKE\_FIND\_USE\_CMAKE\_PATH: 0  
CMAKE\_FIND\_USE\_CMAKE\_ENVIRONMENT\_PATH: 0  
CMAKE\_FIND\_USE\_SYSTEM\_ENVIRONMENT\_PATH: 0  
CMAKE\_FIND\_USE\_CMAKE\_SYSTEM\_PATH: 0 lead to the same toolchain prefix combined with current source dir instead of patch specified by $JAVA\_HOME

---

<div class="post-metadata">

### Author: ![yisseamake](https://discourse.cmake.org/user_avatar/discourse.cmake.org/yisseamake/32/897_2.png) [@yisseamake](https://discourse.cmake.org/u/yisseamake)
#### Post date: [November 17, 2020, 2:25am UTC](https://discourse.cmake.org/t/find-package-jni-not-able-to-find-jni-cmake-v-3-17/2075/4 "2020-11-17T02:25:04Z")

</div>

Probably when CMAKE\_TOOLCHAIN\_FILE and the toolchain .cmake file sets SYSTEM e.g:  
include\_directories( SYSTEM “${ANDROID\_SYSROOT}/usr/include” ${ANDROID\_STL\_INCLUDE\_DIRS} ) then this overrides $JAVA\_HOME settings  
for find\_package(Java) find\_package(JNI). Could cmake team confirm this please? Then a way to enforce $JAVA\_HOME path would be to add it to include\_directories( SYSTEM and documentation has to change appropriately for the FindJNI and Find.Java.
