linux java sys paths,使用java.library区别。路径和LD_LIBRARY_PATH

13

The first form

第一种形式

-Djava.library.path=/path

will be handled in java bytecode level, System.loadLibrary will call Runtime.loadLibary, then will call java/lang/ClassLoader.loadLibrary. In the function call ClassLoader.loadLibrary, the system property java.library.path will be checked to get the full path of the library and pass this full path to native code to call system api dlopen/dlsym, eventually make the library loaded. You can browse the source from OpenJDK repository. The following code snippet is the segment I copy from the link.

将在java字节码级别、系统中处理。loadLibrary将调用运行时。loadLibary,然后将调用java/lang/ClassLoader.loadLibrary。在函数中调用ClassLoader。loadLibrary,系统属性java.library。将检查path以获取库的完整路径,并将此完整路径传递到本机代码,以调用系统api dlopen/dlsym,最终使库被加载。您可以从OpenJDK存储库中浏览源代码。下面的代码片段是我从链接中复制的片段。

The advantage of this form is that you will get error or warning or exception in Java code, if there are some problems with your library path.

这种形式的优点是,如果您的库路径有问题,您将在Java代码中得到错误、警告或异常。

// Invoked in the java.lang.Runtime class to implement load and loadLibrary.

static void loadLibrary(Class fromClass, String name,

boolean isAbsolute) {

ClassLoader loader =

(fromClass == null) ? null : fromClass.getClassLoader();

if (sys_paths == null) {

usr_paths = initializePath("java.library.path");

sys_paths = initializePath("sun.boot.library.path");

}

if (isAbsolute) {

if (loadLibrary0(fromClass, new File(name))) {

return;

}

throw new UnsatisfiedLinkError("Can't load library: " + name);

}

// ....

The second form

第二种形式

export LD_LIBRARY_PATH=/path

will be handled in native, according to the document of dlopen/dlsym

根据dlopen/dlsym的文件,会在本地处理吗

dlopen()

The function dlopen() loads the dynamic library file named by the null-terminated string filename and returns an opaque "handle" for the

dynamic library. If filename is NULL, then the returned handle is for the main program. If filename contains a slash ("/"), then it is

interpreted as a (relative or absolute) pathname. Otherwise, the dynamic linker searches for the library as follows (see ld.so(8) for fur‐

ther details):

o (ELF only) If the executable file for the calling program contains a DT_RPATH tag, and does not contain a DT_RUNPATH tag, then the

directories listed in the DT_RPATH tag are searched.

o If, at the time that the program was started, the environment variable LD_LIBRARY_PATH was defined to contain a colon-separated list of

directories, then these are searched. (As a security measure this variable is ignored for set-user-ID and set-group-ID programs.)

In this manner, if there are some problems with your library path and the system can't load your library, the system won't give too much clue what happen and will fail silently (I guess). It depends whether or not to implement LD_LIBRARY_PATH, Android didn't use LD_LIBRARY_PATH to determine the library location, you can see Android's implementation from here.

通过这种方式,如果您的库路径有问题,并且系统无法加载您的库,那么系统将不会提供太多的线索,并且会无声地失败(我猜)。这取决于是否实现LD_LIBRARY_PATH, Android没有使用LD_LIBRARY_PATH来确定库的位置,从这里可以看到Android的实现。

Caused by: java.lang.UnsatisfiedLinkError: no opencv_java460 in java.library.path: C:\Program Files\Java\jdk-17\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;E:\lin\bin\;C:\Program Files\Common Files\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\MySQL\MySQL Server 8.2\bin;C:\Users\Lenovo\AppData\Local\Programs\Python\Python312;E:\java11\bin;C:\Program Files\Java\jdk-17\bin;E:\apache-jmeter-5.6.2\apache-jmeter-5.6.2\bin;E:\redis\;C:\Program Files\dotnet\;E:\nvm-set\nvm;E:\nvm-set\node.js;E:\maven\apache-maven-3.6.3\bin;D:\Tesseract-OCR;E:\phantomjs-2.1.1-windows\phantomjs-2.1.1-windows\bin;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;D:\rose\common;C:\Program Files\NVIDIA Corporation\NVIDIA app\NvDLISR;E:\apache-tomcat-8.5.97\apache-tomcat-8.5.97\bin;C:\Users\Lenovo\AppData\Local\Programs\Python\Launcher\;C:\Users\Lenovo\AppData\Local\Microsoft\WindowsApps;E:\Microsoft VS Code\bin;C:\Users\Lenovo\AppData\Local\Programs\Python\Python312;E:\java11\bin;E:\node\node_modules;C:\Users\Lenovo\AppData\Roaming\npm;E:\nvm-set\nvm;E:\nvm-set\nodejs;E:\maven\apache-maven-3.6.3\bin;E:\phantomjs-2.1.1-windows\phantomjs-2.1.1-windows\bin;;. at java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2429) at java.base/java.lang.Runtime.loadLibrary0(Runtime.java:818) at java.base/java.lang.System.loadLibrary(System.java:1989) at com.neu.smty.service.FaceCompare.<clinit>(FaceCompare.java:42) at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499) at java.base/java.lang.reflec
03-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值