Documentation is not clear for handling of native libraries in data attribute for java_library and java_binary
A native library like cc_library in the data attribute of java_library will cause the dependent java_binary targets to put a flag like -Djava.library.path=where/to/find/native/lib in the generated wrapper script.
However putting it in data attribute in java_binary has no effect. This should be better documented. In general there should be better documentation for how java interacts with native code.
Things like what does Bazel put in the generated wrapper script and which attribute affects important flags like -Djava.library.path should be clearly written down.
Ouch. But please, don't put cc_library in data.
My recommendation is this:
- if you compile C/C++ code with cc_library, put the cc_library in deps of the java_library; this will implicitly create a .so in any dependent java_binary, which links the code from all transitive cc_library deps; the .so will be put onto the classpath, and Bazel also adds a text file to the classpath with a fixed location that contains the name of the .so, and which can be used to get the name of the library, extract, and load it at runtime
- if you need to control the .so very carefully, or you can't put it on the classpath for some reason, then use a cc_binary named like "libmy_library.so" to build the .so, and then put that into data
- if that also doesn't work, maybe because you need to target Windows, then file a feature request ;-)
Thanks for the answers. My user case fall into the second case. I need to control the name of the so and it needs to be in java.library.path.
Assigning for @spomorski backlog to update/clarify documentation
- if you compile C/C++ code with cc_library, put the cc_library in deps of the java_library; this will implicitly create a .so in any dependent java_binary, which links the code from all transitive cc_library deps; the .so will be put onto the classpath, and
This is how it's supposed to work according to the documentation for java_binary/java_test, but it doesn't as per #3079.
Thank you for contributing to the Bazel repository! This issue has been marked as stale since it has not had any activity in the last 1+ years. It will be closed in the next 90 days unless any other activity occurs. If you think this issue is still relevant and should stay open, please post any comment here and the issue will no longer be marked as stale.
This issue has been automatically closed due to inactivity. If you're still interested in pursuing this, please post @bazelbuild/triage in a comment here and we'll take a look. Thanks!