日期:2014-05-17  浏览次数:20905 次

java web 找不到java.library.path路径
项目是在window下开发的,现在放到linux下部署需要加载一个.so的文件,但是调用
System.loadLibrary("xxxx");main方法可以找到,就在当前目录,但是放到web项目里就找不到这个xxx文件了,报错no xxx in java.library.path
我打印出了这个路径,放进去也找不到!
路径如下:
/usr/java/jdk1.6.0_35/jre/lib/amd64/server:/usr/java/jdk1.6.0_35/jre/lib/amd64:/usr/java/jdk1.6.0_35/jre/../lib/amd64:/usr/local/resin/libexec64:/usr/java/jdk1.6.0_35/jre/lib/amd64/server:/usr/java/jdk1.6.0_35/jre/lib/amd64:/usr/java/jdk1.6.0_35/jre/../lib/amd64:/usr/local/resin/webapps/ROOT/trialSys/trialDBInterface/WEB-INF/classes::/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib 
------解决方案--------------------
加载路径不对。
你开发测试的时候是 eclipse,System.loadLibrary("xxxx") 加载,当然没问题。
但是部署以后,System.loadLibrary("xxxx") 就不行了,你需要这样:
static {
    try {
        System.loadLibrary("crypt"); // used for tests. This library in classpath only
    } catch (UnsatisfiedLinkError e) {
        try {
            NativeUtils.loadLibraryFromJar("/natives/crypt.dll"); // during runtime. .DLL within .JAR
        } catch (IOException e1) {
            throw new RuntimeException(e1);
        }
    }
}


NativeUtils 源码参考:

package cz.adamh.utils;
 
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
 
/**
 * Simple library class for working with JNI (Java Native Interface)
 * 
 * @see http://frommyplayground.com/how-to-load-native-jni-library-from-jar
 *
 * @author Adam Heirnich <adam@adamh.cz>, http://www.adamh.cz
 */
public class NativeUtils {
 
    /**
     * Private constructor - this class will never be instanced
     */
    private NativeUtils() {
    }
 
    /**
     * Loads library from current JAR archive
     * 
     * The file from JAR is copied into system temporary directory and then loaded. The temporary file is deleted after exiting.
     * Method uses String as filename because the pathname is "abstract", not system-dependent.
&nb