在 Linux 上的 Eclipse RCP 应用程序中加载本机库
我有一个 Eclipse RCP 应用程序,它通过 JNI 使用一些本机库.这些是动态链接到彼此的共享库.在 Windows 上,我将这些库(作为 *.dll
文件)放在 RCP 启动器可执行文件(*.exe
)旁边,并通过 System.load("<绝对文件路径>")
.这很好用,因为启动器的位置似乎已添加到 java.library.path
以便库之间的动态链接有效.
I have an Eclipse RCP application that uses some native libraries via JNI. These are shared libraries that dynamically link to each other. On Windows I put these libraries (as *.dll
files) next to the RCP launcher executable (*.exe
) file and load them via System.load("<absolute file path>")
. This works great, as the location of the launcher seems to be added to the java.library.path
so that dynamic linking between the libraries works.
在 Linux 上,我得到一个 UnsatisfiedLinkError
.启动器的位置未添加到 java.library.path
.当我在设置 LD_LIBRARY_PATH
变量后从终端启动应用程序时,它可以工作:
On Linux, I get an UnsatisfiedLinkError
. The location of the launcher is not added to the java.library.path
. When I start the application from the terminal after setting the LD_LIBRARY_PATH
variable it works:
export LD_LIBRARY_PATH=.
./myApp
.
位置被添加到 java.library.path
.有一个更好的方法吗?我希望用户只需双击启动器.
The location .
is the added to the java.library.path
. Is there a better way to do this? I want the users to just double click the launcher.
在 myApp.ini
文件中设置 -Djava.library.path=.
也不起作用.我在安装详细信息中看到了它,但我仍然收到 UnsatisfiedLinkError
.
Setting -Djava.library.path=.
in the myApp.ini
file does also not work. I see it in the installation details but I still get an UnsatisfiedLinkError
.
推荐答案
查找库最可靠的方法是根本不使用 java.library.path
而是通过 Java 代码找到它们并通过加载System.load()
而不是 System.loadLibrary()
.您可以应用您想要的任何逻辑来查找本机库(尽管最好不要太聪明),如果您的机制失败,您可以回退到尝试 java.library.path
.
The most reliable way to find libraries is not using java.library.path
at all but finding them via Java code and load via System.load()
instead of System.loadLibrary()
. You can apply whatever logic you want for finding the native library (although it's probably best trying not to be too clever) and you could fall back to trying java.library.path
if your mechanism fails.
这当然只有在库不依赖于其他可能找不到的库时才有效.
This will only work of course if the library doesn't depend on other libraries that might not be found.
相关文章