How to influence search path of System.loadLibrary() through Java code?

前端 未结 6 1083
一向
一向 2021-02-04 03:18

In a Java project, I am using a third-party library that loads some native library via

System.loadLibrary(\"libName\");

I\'d like to be able to

6条回答
  •  醉话见心
    2021-02-04 03:47

    1. There is no approved way to change the library path for a running JVM.
    2. You cannot load a native library more than once ... and you cannot unload a native library so that you can load it again: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4171986

    Based on your comments above (particularly, the behavior of the 3rd-party library), I'd say that your best option is to get the library path right when you launch the JVM.

    Note that there is a hacky way to change the library path (see https://stackoverflow.com/a/24258955/139985) but it involves nasty reflection, and it reportedly doesn't work for all Java releases. Certainly, it relies on undocumented private implementation details of ClassLoader that could change from one release to the next.

提交回复
热议问题