sigar-amd64-winnt.dll ...无法引用它或将其与.jar捆绑在一起 [英] sigar-amd64-winnt.dll ... can't reference it or bundle it with .jar
问题描述
(这可能是我犯的一个明显的错误,但现在6小时后我无法让它工作。)
我正在尝试在我的eclipse项目中使用sigar.jar获取CPU信息(现在只测试sigar API)。
它在eclipse中运行没有问题:
- 在eclipse中工作的原因是将dll放在与
相同的文件夹中sigar.jar - 我测试了为dll添加路径,因为构建路径配置对话框中的本机代码属性
无效。 - 在运行配置中添加vm参数没有效果。
- 我测试了在这2个地方放置2条假路径,只要我在与sigar.jar相同的文件夹中有
dll ...它在eclipse中运行良好。
问题是当我尝试为我的项目导出一个可运行的.jar文件时。我尝试了两件事:
- 我使用Bundle-NativeCode修改了MANIFEST.MF文件:libs / sigar-amd64-winnt.dll(I假设这里的路径是相对于项目文件夹的 - >没有成功:
主要开始!! java.library.path中没有sigar-amd64-winnt.dll
org.hyperic.sigar.SigarException:
中没有sigar-amd64-winnt.dll java.library.path
at org .hyperic.sigar.Sigar.loadLibrary(Sigar.java:172)
at org.hyperic.sigar.Sigar。< clinit>(Sigar.java:100)
at CpuData.main(CpuData。 java:59)
cpudata(sigar s)开始!!! cpuInfo()开始!!!
线程中的异常
mainjava.lang.UnsatisfiedLinkError:
org.hyperic.sigar.Sigar.getCpuInfoList()[Lorg / hyperic / sigar / C puInfo;
at org.hyperic.sigar.Sigar.getCpuInfoList(Native Method)
at CpuData.cpuInfo(CpuData.java:103)
at CpuData。< init>(CpuData.java:29 )
at CpuData.main(CpuData.java:59)
- 然后我决定将.dll放在与我的project.jar相同的文件夹中,并在命令行中使用:java -Djava.library.path =。/ native / -jar C:\ cpu_usage_log\cpu3。 jar(我假设这里的路径是相对于包含project.jar的文件夹)...但是再次没有成功:
< blockquote>
错误:无法找到或加载主类.library.path = .. native
(我怀疑我应该在-Djava.library.path =。/ path /之后给出一个主类名作为第二个参数但是我找不到那个假定的主类名称,或者在网上做的任何例子指定这样一个类...它是.dll中的主类吗?)
<在这一点上,我真的不知道还有什么可以尝试的。我读过那些为他人工作的2个解决方案,这让它更加令人沮丧,因为我觉得在阅读其他帖子时我错过或听不懂的东西是显而易见的,而我却找不到(这是我第一次交易)在java项目中使用本机dll。)
对我而言,最好修改Java加载库的方式。
通常你调用 System.loadLibrary(mylib);
搜索库路径上的库。
恕我直言,使用它的绝对路径加载库要好得多。这允许您在程序中实现自定义搜索逻辑:
//将名称扩展为mylib.so或mylib.dll
mylibname = System.mapLibraryName(mylib);
//通过绝对路径加载库
System.load(new File(path,mylibname).getAbsolutePath());
请注意,每个库只能加载一次,因此如果您按上图所示加载库,则调用之后 System.loadLibrary(mylib);
将被忽略,因为库已经加载。
(It might be an obvious mistake I'm making, but I couldn't get it to work after 6 hours now.)
I'm trying to get CPU information using the sigar.jar in my eclipse project (just testing the sigar API for now).
It runs in eclipse without problems:
- what made it work in eclipse was to put the dll in the same folder as the sigar.jar
- I tested that adding a path to the dll as the "native code property" in the build path config dialog has no effect.
- Adding vm arguments in the run configuration also has no effect.
- I tested putting 2 fake paths in those 2 places and as long as I have the dll in the same folder as the sigar.jar... it runs well in eclipse.
The problem is when I try to export a runnable .jar file for my project. I tried 2 things:
- I modified the MANIFEST.MF file with Bundle-NativeCode: libs/sigar-amd64-winnt.dll (I'm assuming here the path is relative to the project folder) --> no success:
main starting!! no sigar-amd64-winnt.dll in java.library.path
org.hyperic.sigar.SigarException: no sigar-amd64-winnt.dll in
java.library.path
at org.hyperic.sigar.Sigar.loadLibrary(Sigar.java:172)
at org.hyperic.sigar.Sigar.<clinit>(Sigar.java:100)
at CpuData.main(CpuData.java:59)
cpudata(sigar s) starting!!! cpuInfo() starting!!!
Exception in thread
"main" java.lang.UnsatisfiedLinkError:
org.hyperic.sigar.Sigar.getCpuInfoList()[Lorg/hyperic/sigar/C puInfo;
at org.hyperic.sigar.Sigar.getCpuInfoList(Native Method)
at CpuData.cpuInfo(CpuData.java:103)
at CpuData.<init>(CpuData.java:29)
at CpuData.main(CpuData.java:59)
- Then I decided to put the .dll in the same folder as my project.jar, and used in the command line: java -Djava.library.path=./native/ -jar C:\cpu_usage_log\cpu3.jar (I'm assuming here the path is relative to the folder that contains the project.jar) ...but again no success:
Error: impossible to find or load the main class .library.path=..native
(I suspected that I should give a main class name as a second argument after the -Djava.library.path=./path/ but I can't find that supposed "main class" name, or any examples on the web that do specify such a class ...is it a main class from within the .dll?)
I don't really know what else to try at this point. I read those 2 solutions worked for others, and it makes it even more frustrating because I imagine it could something obvious that I missed or didn't understand when reading other posts and that I just can't find (it's the first time I deal with native dlls in a java project).
For me it was always the best to modify the way how Java loads the library.
Usually you call System.loadLibrary("mylib");
which searches the library on the library path.
IMHO it is much better loading the library using it's absolute path. This allows you to implement a custom search logic in your program:
// Extends the name to mylib.so or mylib.dll
mylibname = System.mapLibraryName("mylib");
// Load the library via its absolute path
System.load(new File(path, mylibname).getAbsolutePath());
Note that each library can only be loaded once, therefore if you load the library as shown above, calls of System.loadLibrary("mylib");
afterwards will be ignored as the library is already loaded.
这篇关于sigar-amd64-winnt.dll ...无法引用它或将其与.jar捆绑在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!