System.loadLibrary does not work. UnsatisfiedLinkError for the second lib in chain

﹥>﹥吖頭↗ 提交于 2019-11-30 07:19:15

The reason for this is that libclient.so is loaded from your JVM, which looks in java.library.path. However, when libclient.so tries to load libhttp.so, it knows nothing about Java and just uses the regular Linux way of loading shared libraries (the dynamic linker ld.so), which looks in LD_LIBRARY_PATH and some common directories like /usr/lib64.

I would probably go with using LD_LIBRARY_PATH set from a start script of your Java application. If you don't want to use a start script, you could in theory set LD_LIBRARY_PATH from within the process itself. However, Java does not allow to do this (there is only System.getenv(), not System.setenv()), so you would need to write a small C library that is called from Java and calls putenv() setting LD_LIBRARY_PATH.

If you build libclient.so itself, you can use the -rpath linker flag to specify a path where the dynamic linker should look for further required libraries. Be careful if you specify a relative path here, it will interpreted as relative to the current working directory of the running application, not relative to the location of libclient.so. To achieve this, you need to use $ORIGIN as argument for -rpath and be careful that your shell does not expand this.

So, if you want to have libclient.so and libhttp.so in the same directory, you need to use

-rpath '$ORIGIN'

as argument to the linker when building libclient.so. If you do not call the linker directly but let your compiler call it, you need to add the following to your compiler's command line:

-Wl,-rpath,'$ORIGIN'

More information about this can be found in the man page for ld.so.

I have no good answer for this question.

But I found several good ways.

  1. Put libhttp.so into shared location for libraries, for example /usr/lib64.
  2. Put path to libhttp.so into LD_LIBRARY_PATH.
  3. Build libclient.so with libhttp.so inside.
  4. Use -rpath during building of libclient.so.

For correct lookup of library (from java.library.path) for different OS's must have different names:

  • Linux: libhttp.so
  • Windows: http.dll

Than you can call from Java:

System.loadLibrary( "http" );
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!