JNI 设置库文件路径

1. Java的System.load 和 System.loadLibrary都可以用来加载库文件
2.例如你可以这样载入一个windows平台下JNI库文件:
System.load("C://Documents and Settings//TestJNI.dll"); //绝对路径
3. System.loadLibrary参数为库文件名

例如你可以这样载入一个windows平台下JNI库文件

System.loadLibrary ("TestJNI");

这里TestJNI必须在 java.library.path这一jvm变量所指向的路径中,可以通过如下方法获得该变量的值:

System.getProperty("java.library.path");

默认情况下,Windows平台下包含下面的路径:

1)和jre相关的目录
2)程序当前目录
3)Windows目录
4)系统目录(system32)
5)系统环境变量path指定的目录

4.在linux下添加一个java.library.path的方法如下:

在/etc/profile 后面加上一行 export LB_LIBRARY_PATH=路径

5.在执行程序的时候可以显示指定,-Djava.library.path=路径

这种会清除掉预设置的java.library.path的值 。实例如下:

java -jar -Djava.library.path=/home/fly/Desktop/sound_dream sound.war

JNA 设置库文件路径


Make your target library available to your Java program. There are several ways to do this:

1. The preferred method is to set the jna.library.path system property to the path to your target library. This property is similar to java.library.path, but only applies to libraries loaded by JNA.
2. Change the appropriate library access environment variable before launching the VM. This is PATH on Windows, LD_LIBRARY_PATH on Linux, and DYLD_LIBRARY_PATH on OSX.
3. Make your native library available on your classpath, under the path {OS}-{ARCH}/{LIBRARY}, where {OS}-{ARCH} is JNA's canonical prefix for native libraries (e.g. win32-x86, linux-amd64, or darwin). If the resource is within a jar file it will be automatically extracted when loaded.


本博客所有文章除特别声明外,均采用 CC BY-SA 3.0协议 。转载请注明出处!