Bazel: Java app with JNI dependency(Bazel:具有 JNI 依赖项的 Java 应用程序)
问题描述
我已经设法构建了我的 JNI 库(jar、jni 共享的 cc_library、包装的 cc_library),但我不知道如何构建使用它的 Java 应用程序.我的 BUILD 很简单:
I've managed to build my JNI library (the jar, the jni shared cc_library, the wrapped cc_library) but I don't see how to build a Java app that uses it. My BUILD is simple:
java_binary(
name = "OCFTestServer",
srcs = glob(["src/main/java/**/*.java"]),
deps = ["//:OpenOCF-JNI"])
这里的 OpenOCF-JNI
看起来像这样:
Here OpenOCF-JNI
looks like this:
java_library(
name = "OpenOCF-JNI",
srcs = glob(["src/main/**/*.java"]),
deps = ["libopenocf"],
visibility = ["//visibility:public"])
而libopenocf
是:
cc_library(
name = "libopenocf",
srcs = glob(["src/c/*.c"]) + glob(["src/c/*.h"])
+ ["@local_jdk//:jni_header",
"@local_jdk//:jni_md_header-darwin"],
... etc ...
这些都成功构建.但是,构建不会导致构建依赖项,这是我所期望的(即构建 OCFTestServer 应该会导致构建 OpenOCF-JNI,这应该会导致构建 libopenocf-jni).这不应该发生吗?
These all build successfully. However, building does not cause dependencies to be built, which is what I would expect (i.e. building OCFTestServer should cause OpenOCF-JNI to be built, which should cause libopenocf-jni to be built). Shouldn't that happen?
如果我使用单独的步骤构建它们,然后尝试运行应用程序(使用 bazel-bin 中的 OCFTestServer 包装器),我会得到 UnsatisfiedLinkError: no libopenocf-jni in java.library.path
.但通过阅读文档,我得到的印象是这一切都应该自动设置(即所需的 jni 库应该放在 java.library.path 中).
If I build them all using separate steps and then try to run the application (using the OCFTestServer wrapper in bazel-bin), I get UnsatisfiedLinkError: no libopenocf-jni in java.library.path
. But from reading the docs I get the impression that this should all be set up automatically (i.e. the needed jni lib should be put in the java.library.path).
我做错了什么?有人有构建和使用 JNI 库的示例吗?
What am I doing wrong? Does anybody have an example of building and using a JNI lib?
推荐答案
我创建了一个简单的 repo:https://github.com/mhlopko/bazel-jni-example 帮助您入门.
I created a simple repo: https://github.com/mhlopko/bazel-jni-example to help you get started.
构建
:
cc_library(
name = "main-jni-lib",
srcs = [
"@local_jdk//:jni_header",
"@local_jdk//:jni_md_header-linux",
"Main.cc"
],
hdrs = [ "Main.h" ],
includes = [ "external/local_jdk/include", "external/local_jdk/include/linux" ],
)
cc_binary(
name = "libmain-jni.so",
deps = [ ":main-jni-lib" ],
linkshared = 1,
)
java_binary(
name = "Main",
srcs = [ "Main.java" ],
main_class = "Main",
data = [ ":libmain-jni.so" ],
jvm_flags = [ "-Djava.library.path=." ],
)
Main.java
:
public class Main {
static {
System.loadLibrary("main-jni");
}
private native int foo();
public static void main(String[] args) {
System.out.println(new Main().foo());
}
}
Main.h
:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class Main */
#ifndef _Included_Main
#define _Included_Main
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: Main
* Method: foo
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_Main_foo(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
Main.cc
:
#include <jni.h>
#include <stdio.h>
#include "Main.h"
JNIEXPORT jint JNICALL Java_Main_foo(JNIEnv *, jobject) {
return 42;
}
现在通过运行 bazel run :Main
你应该看到 42
打印出来了,它来自 Main.cc
.该示例显然需要更多修饰,因此它可以在非 linux 平台上运行,因此它可以与启动器脚本一起使用.您可能最终需要多个 System.loadLibrary
调用,就像 bazel 在 它的 windows 加载器.
Now by running bazel run :Main
you should see 42
printed out, that is coming from Main.cc
. The example clearly needs more polish so it works on other-than-linux platforms, and so it works with the launcher script. You might end up needing multiple System.loadLibrary
calls, like bazel does in its windows loader.
这篇关于Bazel:具有 JNI 依赖项的 Java 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Bazel:具有 JNI 依赖项的 Java 应用程序
基础教程推荐
- 如何强制对超级方法进行多态调用? 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01