Java Attach API:动态更改 java.library.path

2023-08-24Java开发问题
22

本文介绍了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:

  1. 我的机器配置不正确,$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.

这篇关于Java Attach API:动态更改 java.library.path的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13