Load Native Library from Class path

前端 未结 3 1549
孤独总比滥情好
孤独总比滥情好 2020-12-06 20:03

I have a project setup that follows the Standard Directory Layout (not using Maven though):

src/main
      | java
      | resources
         | library.dll
<         


        
3条回答
  •  被撕碎了的回忆
    2020-12-06 20:12

    There is an old-time hack that still works as of today ( 1.7.0_55 & 1.8.0_05 ) to allow you to do to a runtime update using System.setProperty() and have the JVM notice the change. In general, you do the following:

    System.setProperty("java.library.path", yourPath);
    Field sysPath = ClassLoader.class.getDeclaredField( "sys_paths" );
    sysPath.setAccessible( true );
    sysPath.set( null, null );
    System.loadLibrary(libraryName);
    

    Google java sys_paths and look for articles about this technique.

    Take care to handle errors/exceptions. Restore original paths as needed.

提交回复
热议问题