java 从 jar 加载库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4113317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Load library from jar
提问by Siassei
how I can load a library from a jar? e.g.
如何从 jar 加载库?例如
Test.jar
+- libAbc.so
+- libDef.so
+- ...
回答by Kevin Day
Probably not the answer you are looking for (extracting the file to a temp location isthe answer you are looking for), but I thought I'd share some real world experience:
可能不是您正在寻找的答案(将文件提取到临时位置就是您正在寻找的答案),但我想我会分享一些现实世界的经验:
The plumbing required to exctract the library from jar, make sure it gets cleaned up afterwards, doesn't conflict with other applications that might be using the jar, etc... is very tricky. It can be done, but chances are very good that you'll wind up with either tons of temp copies of your libraries cluttering the user's system, or you'll wind up with conflicts between multiple apps using the libraries.
从 jar 中提取库所需的管道,确保它之后被清理,不与可能使用 jar 的其他应用程序发生冲突,等等......非常棘手。这是可以做到的,但是很有可能您最终会遇到大量库的临时副本使用户的系统变得混乱,或者您最终会在使用这些库的多个应用程序之间发生冲突。
When you add the fact that many operating systems don't allow just any file to be used as a library (and frequently, the permissions for the user who will be running your app do not allow them to mark an arbitrary file in the temp folder for execution), the idea of packaging the native libraries inside the jar becomes less attractive.
当您添加一个事实时,许多操作系统不允许将任何文件用作库(并且经常,将运行您的应用程序的用户的权限不允许他们标记临时文件夹中的任意文件执行),将本地库打包在 jar 中的想法变得不那么有吸引力了。
What we finally wound up doing was shifting to a model where we have our installer (which doesrun with sufficient permissions) place the appropriate native library alongside the jar. This is fairly simple to do, keeps all executing libraries in the same place, and is easy to administer and understand (trying to track down a version incompatibility in one application b/c it is trying to load a library that was saved to temp storage by a second application is a total nightmare).
我们最终做的是转移到一个模型,我们让我们的安装程序(它确实以足够的权限运行)将适当的本机库放在 jar 旁边。这相当简单,将所有正在执行的库保存在同一个位置,并且易于管理和理解(试图在一个应用程序 b/c 中追踪版本不兼容,它正在尝试加载已保存到临时存储的库通过第二个应用程序是一个彻头彻尾的噩梦)。
回答by Peter Knego
You have to unpack it into some temporary dir and the use `System.load("path/to/libAbc.so").
您必须将其解压到某个临时目录中并使用`System.load("path/to/libAbc.so")。
This has been discussed previously.
Edited: corrected the link.
编辑:更正了链接。
回答by Thorbj?rn Ravn Andersen
Native code is being loaded by the underlying operating system, and if that code cannot peek inside a jar-file to pick up the bytes you want to load - which Windows cannot - you must make it accessible in the file system yourself.
本机代码正在由底层操作系统加载,如果该代码无法在 jar 文件中查看以获取您想要加载的字节 - Windows 不能 - 您必须自己在文件系统中访问它。
Can you deploy your library next to your jar file?
你能在你的 jar 文件旁边部署你的库吗?
回答by splash
Libraries must be loaded from the filesystem. So you could extract your library from the jar into a temp directory and load it.
库必须从文件系统加载。所以你可以将你的库从 jar 中提取到一个临时目录中并加载它。