rules_jni provides access to commonly used parts of the Java Native Interface (JNI) and convenience Java classes.
Full label: @fmeum_rules_jni//jni
(long form: @fmeum_rules_jni//jni:jni
)
This target can be added to the deps
of a cc_*
rule to get access to the jni.h
header, which can be included via:
#include <jni.h>
The correct version of the OS-specific jni_md.h
header used internally by jni.h
is inferred automatically from the
current target platform.
Full labels: @fmeum_rules_jni//jni:libjvm
, @fmeum_rules_jni//jni:libjvm_lite
This library can be added to the deps
of a cc_*
target that requires access to
the Java Invocation API.
This target serves as a drop-in replacement for the jvm
shared library contained in JDKs and JREs, which is
called jvm.dll
, libjvm.dylib
or libjvm.so
depending on the OS. These libraries are usually not available in the standard locations searched by the
dynamic linker, which makes it difficult to run binaries linked against the original libraries on other machines.
The libjvm
target solves this problem by providing a small stub library that locates and loads the real library at
runtime when its first symbol is accessed. Concretely, it does the following in order:
-
(
libjvm
only) When invoked viabazel test
, as part of an action during the build or in any other situation where the Bazel C++ runfiles library finds a runfiles directory or manifest, locate the current Bazel Java runtime in the runfiles and look for thejvm
shared library relative to it at well-known locations. If it cannot be found, print a warning and continue.Note: To ensure that the current Bazel Java runtime is also found if the binary is executed via
bazel run
or directly frombazel-bin
and to ensure that Java code can find its runfiles, do the following:- Depend on
@fmeum_rules_jni//jni:libjvm
directly from the top-levelcc_binary
. - Add
#include <rules_jni.h>
. - Call
rules_jni_init(const char* argv0)
from themain
function, providingargv[0]
as the argument. This call is not thread-safe as it modifies the environment.
If you want this lookup to succeed also for binaries executed by other binaries that are themselves run from Bazel, set the environment variables required for runfiles discovery.
- Depend on
-
Dynamically load the
jvm
shared library directly using the standard linker search path. -
If
JAVA_HOME
is set, find thejvm
shared library relative to it at well-known locations, exiting if it cannot be found. -
(macOS only) Execute
/usr/libexec/java_home
and use its output as a replacement forJAVA_HOME
. -
If
PATH
is set, find the Java binary (java
orjava.exe
) on it and try to load thejvm
shared library from well-known locations relative to it, exiting if it cannot be found.
To get detailed runtime logs from this location procedure, set the environment variable RULES_JNI_TRACE
to a non-empty
value.
Note: libjvm
depends on
the Bazel C++ runfiles library and
thus on a C++ standard library. For C-only projects or release builds that are only run outside Bazel, consider
using libjvm_lite
instead.
Full label: @fmeum_rules_jni//jni/tools/native_loader
This library can be added to the deps
of a java_*
target and offers static methods that can be used to load native
libraries created with cc_jni_library
at runtime. See its
javadocs for details.