在Linux上的Eclipse RCP应用程序中加载本机库

时间:2022-06-16 10:45:35

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.

我有一个Eclipse RCP应用程序,它通过JNI使用一些本机库。这些是动态链接到彼此的共享库。在Windows上,我将这些库(作为* .dll文件)放在RCP启动程序可执行文件(* .exe)文件旁边,并通过System.load(“ ”)加载它们。这很好用,因为启动器的位置似乎被添加到java.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:

在Linux上,我得到一个UnsatisfiedLinkError。启动程序的位置未添加到java.library.path中。当我在设置LD_LIBRARY_PATH变量后从终端启动应用程序时,它可以工作:

export LD_LIBRARY_PATH=.
./myApp

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.

那个地点 。是添加到java.library.path。有一个更好的方法吗?我希望用户只需双击启动器。

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.

设置-Djava.library.path =。在myApp.ini文件中也不起作用。我在安装细节中看到它,但我仍然得到一个UnsatisfiedLinkError。

1 个解决方案

#1


2  

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.

查找库的最可靠方法是不使用java.library.path,而是通过Java代码查找它们,并通过System.load()而不是System.loadLibrary()加载。您可以应用您想要的任何逻辑来查找本机库(尽管最好不要太聪明),如果您的机制失败,您可以回到尝试java.library.path。

This will only work of course if the library doesn't depend on other libraries that might not be found.

只有当库不依赖于可能找不到的其他库时,这才会起作用。

#1


2  

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.

查找库的最可靠方法是不使用java.library.path,而是通过Java代码查找它们,并通过System.load()而不是System.loadLibrary()加载。您可以应用您想要的任何逻辑来查找本机库(尽管最好不要太聪明),如果您的机制失败,您可以回到尝试java.library.path。

This will only work of course if the library doesn't depend on other libraries that might not be found.

只有当库不依赖于可能找不到的其他库时,这才会起作用。