Java Attach API:动态更改 java.library.path
在我的 Windows 机器上使用 com.sun.tools.attach API 时,我在调用时收到以下错误
When using the com.sun.tools.attach API on my Windows machine, I get the following error when making a call to
VirtualMachine.list()
java.lang.UnsatisfiedLinkError:java.library.path 中没有附加
原因是缺少 attach.dll.attach.dll 位于 $JRE/bin/
中.当使用 -Djava.library.path=[Directory to the attach.dll]
启动我的 Java 程序时,一切正常,没有错误输出.
The reason is the missing attach.dll. The attach.dll is located in $JRE/bin/
. When starting my Java program with -Djava.library.path=[Directory to the attach.dll]
everything works out without error output.
现在,我不想在每个 Java 程序启动时都添加这个丑陋的 JVM 参数.因此我的问题是:
Now, I don't want to add every Java program start this ugly JVM parameter. Therefore my questions are:
我的机器配置不正确,
$JRE/bin/
应该在库路径中吗?
如果没有,如何动态添加路径?System.setProperties("java.library.path",StringOfThePathToTheAttach.dll);
行不通.库路径已更改,但仍然出现错误.这是否与 SecurityManager 或 JVM 启动有关?
If not, how can I add the path dynamically? System.setProperties("java.library.path",StringOfThePathToTheAttach.dll);
does not work out. The library path is changed, but the error apperas anyway. Has this something to do with SecurityManager or JVM start up?
推荐答案
您的 System.setProperty("java.library.path", StringOfThePathToTheAttach.dll);
应该可以工作.我的猜测是你打电话太晚了.换句话说,在您设置属性之前尝试访问 DLL.
Your System.setProperty("java.library.path", StringOfThePathToTheAttach.dll);
should work. My guess is that you're calling it too late. In other words, there is an attempt to access the DLL prior to you setting the property.
您能否在代码中设置属性之后以及在违规方法调用之前再次输出 java.library.path 的当前值?
Can you output the current value for java.library.path after the property is set in code and again before the offending method call?
即如果您在看到设置属性后"之前看到在 attach.dll 调用之前"输出,那么您就知道问题出在哪里了.
i.e. If you see "Before attach.dll call" output prior to seeing "After setting property", you know where your problem is.
指向本机库的更好方法是使用 System.load(StringOfThePathToTheAttach.dll)
- 再次,在有问题的代码行之前.
A better way to point to native libraries is to use System.load(StringOfThePathToTheAttach.dll)
- again, before the offending line of code.
相关文章