如何动态加载AttachProvider(attach.dll) [英] How to load AttachProvider (attach.dll) dynamically
问题描述
我正在使用jdk的 tools.jar
中的 com.sun.tools.attach
,它需要一个指定的 java.library.path
env在启动时指向 attach.dll
以正确地调整提供程序,例如 WindowsAttachProvider
。由于某些原因,我需要动态加载捆绑的 attach.dll
。我试图使用这样的一些:
public static void main(String [] args)throws Exception {
Path bin = Paths.get(System.getProperty(user.dir),bin)。toAbsolutePath();
switch(System.getProperty(os.arch)){
caseamd64:
bin = bin.resolve(win64);
break;
默认值:
bin = bin.resolve(win32);
}
// java.library.path的动态设置似乎不够
System.setProperty(java.library.path,System.getProperty(java.library.path )+ File.pathSeparator + bin.toString());
//所以我尝试手动加载attach.dll。这还不够。
System.load(bin.resolve(attach.dll)。toString());
//我在我的应用程序中使用com.sun.tools.attach
new myApp();
}
如果我从jdk(在normall jre)中运行它,它会向我报告:
java.util.ServiceConfigurationError:com.sun.tools.attach.spi.AttachProvider:
Provider sun。 tools.attach.WindowsAttachProvider无法实例化:
java.lang.UnsatisfiedLinkError:no attach in java.library.path
线程main中的异常com.sun.tools.attach.AttachNotSupportedException:
无提供商
在com.sun.tools.attach.VirtualMachine.attach(...
如何在启动时指定 -Djava.library.path
来指向 attach.dll
?
您正在使用的API是使用 loadLibrary(String)看来,你不能成功地抢先(使它成功)这通过调用更多的ex合并 load(String) first。
所以你必须在 java.library.path
中指定路径。 >
该系统属性在JVM生命周期早期设置,并且不能通过标准方法进行修改。
所以常规解决方案将当您启动JVM时,要通过适当的 java.library.path
。
或者,您可以查看在JVM启动后使用反射更改此属性的黑客。我没有尝试任何这些。
例如,请参阅这里:
System.setProperty(java.library.path,/ path / to / libs );
字段fieldSysPath = ClassLoader.class.getDeclaredField(sys_paths);
fieldSysPath.setAccessible(true);
fieldSysPath.set(null,null);
BTW,我建议将您的自定义路径预先挂载到现有路径,而不是替换它。
I'm using com.sun.tools.attach
from jdk's tools.jar
and it need an specified java.library.path
env pointing to attach.dll
at startup to properly instatiate provider such as WindowsAttachProvider
. For some reasons I need to dynamic loading one of bundled attach.dll
. I'm try to use some like this:
public static void main(String[] args) throws Exception {
Path bin = Paths.get(System.getProperty("user.dir"),"bin").toAbsolutePath();
switch (System.getProperty("os.arch")) {
case "amd64":
bin = bin.resolve("win64");
break;
default:
bin = bin.resolve("win32");
}
// Dynamic setting of java.library.path only seems not sufficient
System.setProperty("java.library.path", System.getProperty("java.library.path") + File.pathSeparator + bin.toString());
// So I try to manual loading attach.dll. This is not sufficient too.
System.load(bin.resolve("attach.dll").toString());
// I'm using com.sun.tools.attach in my app
new myApp();
}
If I run this out of jdk (in normall jre) it's report to me:
java.util.ServiceConfigurationError: com.sun.tools.attach.spi.AttachProvider:
Provider sun.tools.attach.WindowsAttachProvider could not be instantiated:
java.lang.UnsatisfiedLinkError: no attach in java.library.path
Exception in thread "main" com.sun.tools.attach.AttachNotSupportedException:
no providers installed
at com.sun.tools.attach.VirtualMachine.attach(...
How to install attach provider without specifying -Djava.library.path
to point attach.dll
at startup?
The API you are using is using loadLibrary(String). It seems you cannot successfully pre-empt (cause it to succeed) this by invoking the more explicit load(String) first.
So you must to specify the path in java.library.path
.
That System property is set once early in JVM lifecycle and is not modifiable by standard means.
So the conventional solution will be to pass an appropriate java.library.path
when you launch the JVM.
Alternatively, you could look into the hacks to change this property after JVM startup using reflection. I have not tried any of these.
For example, see here:
System.setProperty( "java.library.path", "/path/to/libs" );
Field fieldSysPath = ClassLoader.class.getDeclaredField( "sys_paths" );
fieldSysPath.setAccessible( true );
fieldSysPath.set( null, null );
BTW, I would recommend pre-pending your custom path to the existing path, rather than replacing it.
这篇关于如何动态加载AttachProvider(attach.dll)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!