Java: load a library that depends on other libs

后端 未结 3 792
孤城傲影
孤城傲影 2021-01-01 06:47

I want to load my own native libraries in my java application. Those native libraries depend upon third-party libraries (which may or may not be present when my application

3条回答
  •  旧巷少年郎
    2021-01-01 07:12

    This is a hack used to manipulate JVM's library path programmatically. NOTE: it relies on internals of ClassLoader implementation so it might not work on all JVMs/versions.

    String currentPath = System.getProperty("java.library.path");
    System.setProperty( "java.library.path", currentPath + ":/path/to/my/libs" );
    
    // this forces JVM to reload "java.library.path" property
    Field fieldSysPath = ClassLoader.class.getDeclaredField( "sys_paths" );
    fieldSysPath.setAccessible( true );
    fieldSysPath.set( null, null );
    

    This code uses UNIX-style file path separators ('/') and library path separator (':'). For cross-platform way of doing this use System Properties to get system-specific separators: http://download.oracle.com/javase/tutorial/essential/environment/sysprop.html

提交回复
热议问题