FindJNI
Finds the Java Native Interface (JNI) include directories and libraries:
find_package(JNI [<version>] [COMPONENTS <components>...] [...])
JNI enables Java code running in a Java Virtual Machine (JVM) or Dalvik Virtual Machine (DVM) on Android to call and be called by native applications and libraries written in other languages such as C and C++.
This module finds if Java is installed and determines where the include files and libraries are. It also determines what the name of the library is.
Added in version 3.24: Imported targets, components, and Android NDK support.
When using Android NDK, the corresponding package version is reported and a specific release can be requested. At Android API level 31 and above, the additional NativeHelper component can be requested. NativeHelper is also exposed as an implicit dependency of the JVM component (only if this does not cause a conflict) which provides a uniform access to JVM functions.
Components
This module supports optional components, which can be specified with the find_package() command:
find_package(JNI [COMPONENTS <components>...])
Supported components include:
-
AWT -
Added in version 3.24.
Finds the Java Abstract Window Toolkit (AWT).
-
JVM -
Added in version 3.24.
Finds the Java Virtual Machine (JVM).
-
NativeHelper -
Added in version 3.24.
Finds the NativeHelper library on Android (
libnativehelper.so), which exposes JVM functions such asJNI_CreateJavaVM().
If no components are specified, the module defaults are:
- When targeting Android with API level 31 and above: module looks for the
NativeHelpercomponent. For other Android API levels, components are by default not set. - When targeting other systems: module looks for
AWTandJVMcomponents.
Imported Targets
This module provides the following Imported Targets:
-
JNI::JNI -
Added in version 3.24.
Main target encapsulating all JNI usage requirements, available if
jni.his found. -
JNI::AWT -
Added in version 3.24.
Target encapsulating the Java AWT Native Interface (JAWT) library usage requirements, available if the
AWTcomponent is found. -
JNI::JVM -
Added in version 3.24.
Target encapsulating the Java Virtual Machine (JVM) library usage requirements, available if component
JVMis found. -
JNI::NativeHelper -
Added in version 3.24.
Target encapsulating the NativeHelper library usage requirements, available when targeting Android API level 31 and above, and the library is found.
Result Variables
This module defines the following variables:
-
JNI_FOUND -
Boolean indicating whether (the requested version of) JNI was found.
-
JNI_<component>_FOUND -
Added in version 3.24.
Boolean indicating whether the
<component>was found. -
JNI_VERSION -
Full Android NDK package version (including suffixes such as
-beta3and-rc1) or undefined otherwise. -
JNI_VERSION_MAJOR -
Added in version 3.24.
Android NDK major version or undefined otherwise.
-
JNI_VERSION_MINOR -
Added in version 3.24.
Android NDK minor version or undefined otherwise.
-
JNI_VERSION_PATCH -
Added in version 3.24.
Android NDK patch version or undefined otherwise.
-
JNI_INCLUDE_DIRS -
The include directories needed to use the JNI.
-
JNI_LIBRARIES -
The libraries (JAWT and JVM) needed to link against to use JNI.
Cache Variables
The following cache variables are also available to set or use:
-
JAVA_INCLUDE_PATH -
The directory containing the
jni.hheader. -
JAVA_INCLUDE_PATH2 -
The directory containing machine-dependent headers
jni_md.handjniport.h. This variable is defined only ifjni.hdepends on one of these headers. In contrast, Android NDKjni.hcan be typically used standalone. -
JAVA_AWT_INCLUDE_PATH -
The directory containing the
jawt.hheader. -
JAVA_AWT_LIBRARY -
The path to the Java AWT Native Interface (JAWT) library.
-
JAVA_JVM_LIBRARY -
The path to the Java Virtual Machine (JVM) library.
Hints
This module accepts the following variables:
-
JAVA_HOME -
The caller can set this variable to specify the installation directory of Java explicitly.
Examples
Finding JNI and linking it to a project target:
find_package(JNI) target_link_libraries(project_target PRIVATE JNI::JNI)
Finding JNI with AWT component specified and linking them to a project target:
find_package(JNI COMPONENTS AWT) target_link_libraries(project_target PRIVATE JNI::JNI JNI::AWT)
A more common way to use Java and JNI in CMake is to use a dedicated UseJava module:
find_package(Java) find_package(JNI) include(UseJava)
See Also
- The
FindJavamodule to find Java runtime tools and development components. - The
UseJavamodule to use Java in CMake.
© 2000–2026 Kitware, Inc. and Contributors
Licensed under the BSD 3-clause License.
https://cmake.org/cmake/help/latest/module/FindJNI.html