How to load AttachProvider (attach.dll) dynamically

后端 未结 1 1566
说谎
说谎 2021-02-10 07:33

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 a

相关标签:
1条回答
  • 2021-02-10 08:14

    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.

    0 讨论(0)
提交回复
热议问题