windows Java 附加 API:动态更改 java.library.path

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5565356/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:34:17  来源:igfitidea点击:

Java Attach API: changing java.library.path dynamically

javawindowsjvm

提问by Konrad Reiche

When using the com.sun.tools.attach API on my Windows machine, I get the following error when making a call to

在我的 Windows 机器上使用 com.sun.tools.attach API 时,我在调用

VirtualMachine.list()

java.lang.UnsatisfiedLinkError: no attach in java.library.path

java.lang.UnsatisfiedLinkError:java.library.path 中没有附加

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.

原因是缺少attach.dll。attach.dll 位于$JRE/bin/. 当启动我的 Java 程序时,-Djava.library.path=[Directory to the attach.dll]一切正常,没有错误输出。

Now, I don't want to add every Java program start this ugly JVM parameter. Therefore my questions are:

现在,我不想添加每个 Java 程序启动这个丑陋的 JVM 参数。因此我的问题是:

  1. Is my machine not configured right and the $JRE/bin/should be in the library path anyway?

  2. 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?

  1. 我的机器是否配置不正确,$JRE/bin/无论如何都应该在库路径中?

  2. 如果没有,如何动态添加路径?System.setProperties("java.library.path",StringOfThePathToTheAttach.dll);行不通。库路径已更改,但错误仍然存​​在。这与 SecurityManager 或 JVM 启动有关吗?

采纳答案by Teddy Yueh

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.

System.setProperty("java.library.path", StringOfThePathToTheAttach.dll);应该工作。我的猜测是你打电话太晚了。换句话说,在您设置属性之前会尝试访问 DLL。

Can you output the current value for java.library.path after the property is set in code and again before the offending method call?

您能否在代码中设置属性之后以及在调用违规方法之前再次输出 java.library.path 的当前值?

i.e. If you see "Before attach.dll call" output prior to seeing "After setting property", you know where your problem is.

即,如果您在看到“设置属性之后”之前看到“在 attach.dll 调用之前”输出,那么您就知道问题出在哪里。

Edit:

编辑:

A better way to point to native libraries is to use System.load(StringOfThePathToTheAttach.dll)- again, before the offending line of code.

指向本机库的更好方法是使用System.load(StringOfThePathToTheAttach.dll)- 再次,在有问题的代码行之前。

回答by nabito

Just found a link that might answer your question

刚刚找到一个可以回答您问题的链接

"The java.library.path is read only once when the JVM starts up. If you change this property using System.setProperty, it won't make any difference."

“当 JVM 启动时,java.library.path 只被读取一次。如果你使用 System.setProperty 改变这个属性,它不会有任何区别。”

http://fahdshariff.blogspot.jp/2011/08/changing-java-library-path-at-runtime.html

http://fahdshariff.blogspot.jp/2011/08/changed-java-library-path-at-runtime.html

回答by Joe

System.setProperty("java.library.path", System.getProperty("java.library.path") + File.pathSeparator + FOLDER_THAT_CONTAINS_ATTACH_DLL);