cc_binary + libjvm.so i.e. how to build and run a JNI C++ app.

287 views
Skip to first unread message

Pete Stevenson

unread,
Sep 22, 2021, 5:44:26 PM9/22/21
to bazel-...@googlegroups.com, Yaxiong Zhao, Ryan Cheng, Omid Azizi
Hi team at Bazel,

We are trying to use the JNI invocation API [1]. For this, we need to depend on a few JNI headers [2] and also libjvm.so. Specifically, we want to build a binary (using cc_binary) that creates the jvm and invokes a method inside of some compiled java code (in a .jar file, e.g.).

What is the correct way to depend on this collateral? In particular, is there an example on github or anywhere online that shows a cc_binary that depends on libjvm.so?

Thank you,
Pete Stevenson

[1] The JNI invocation API is referenced from the bazel docs, here:
https://docs.bazel.build/versions/main/be/java.html#java_binary.launcher

[2] We need the following headers:
jni.h, jvmti.h, and jni_md.h

Fabian Meumertzheim

unread,
Sep 23, 2021, 12:28:03 PM9/23/21
to bazel-discuss

I didn't find a good answer and set up a repository rule to find libjvm for Jazzer: https://github.com/CodeIntelligenceTesting/jazzer/blob/main/bazel/local_jdk_libjvm.bzl
The cc_library targets are in //driver.
 
This breaks with cross-compilation though, so I would really like to find a better solution.

Fabian Meumertzheim

unread,
Nov 9, 2021, 8:31:38 AM11/9/21
to bazel-discuss
I created a new ruleset, rules_jni (https://github.com/fmeum/rules_jni), to simplify building applications that use libjvm, JNI headers and Java native libraries. It can find libjvm at build and runtime, but within Bazel and in a deployment. 

An example using this ruleset can be found at https://github.com/fmeum/rules_jni/tree/main/tests/libjvm_stub.

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/1ddbf618-c792-47d3-82d3-75775e448488n%40googlegroups.com.

Philipp Wollermann

unread,
Nov 9, 2021, 8:32:58 AM11/9/21
to bazel-discuss
That looks very cool and useful, thank you Fabian!
Reply all
Reply to author
Forward
0 new messages