gpt4 book ai didi

java - 如何打包静态加载的JNI库?

转载 作者:太空宇宙 更新时间:2023-11-04 11:18:12 28 4
gpt4 key购买 nike

我目前有一个 Java 应用程序,它可以动态加载到两个不同平台(Windows 和 Linux)上的 native 库中。我针对这两个系统构建了相同的库:library.dll 和 liblibrary.so。

我必须将这个东西发送给客户,这样我就可以使用 IntelliJ 打包 .msi 和 .deb 工件。现在,我更希望这一切都在幕后处理,而不是将这些包与相应的动态库一起发送并指示客户将它们放入他们的 windows/system32 和/usr/lib/目录中。我想有某种方法可以将动态库打包到我的 .msi 或 .deb 中,并指示这些安装程序将库复制到正确的文件夹,但我担心权限错误和类似的事情。

我读到 Java 1.8 现在支持静态库,但令人震惊的是缺乏关于如何实现这一点的文档。大家都同意,您需要在库中公开两个新调用:JNI_OnLoad_LibraryNameJNI_OnUnload_LibraryName,并且您只需像往常一样调用 System.loadLibrary("libraryName") 即可。然而,似乎没有人提到或至少给出如何将静态库与 JVM 一起打包的说明。

关于添加此功能的 Java 文档简单地指出:

A native JVMTI Agent may be statically linked with the VM. The manner in which the library and VM image are combined is implementation-dependent.

我是否只是没有寻找正确的术语来了解如何将静态 native 库打包到 JVM 中?我应该为静态库烦恼吗?我缺少一些更简单的解决方案吗?

最佳答案

https://www.adamheinrich.com/blog/2012/12/how-to-load-native-jni-library-from-jar/

以下代码用于初始化库:

static {
try {
System.loadLibrary("crypt"); // used for tests. This library in classpath only
} catch (UnsatisfiedLinkError e) {
try {
NativeUtils.loadLibraryFromJar("/natives/crypt.dll"); // during runtime. .DLL within .JAR
} catch (IOException e1) {
throw new RuntimeException(e1);
}
}
}

关于java - 如何打包静态加载的JNI库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45220838/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com