java中library找不到了,java web 找不到java.library.path途径

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 , 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.

*

* @param filename The filename inside JAR as absolute path (beginning with '/'), e.g. /package/File.ext

* @throws IOException If temporary file creation or read/write operation fails

* @throws IllegalArgumentException If source file (param path) does not exist

* @throws IllegalArgumentException If the path is not absolute or if the filename is shorter than three characters (restriction of {@see File#createTempFile(java.lang.String, java.lang.String)}).

*/

public static void loadLibraryFromJar(String path) throws IOException {

if (!path.startsWith("/")) {

throw new IllegalArgumentException("The path to be absolute (start with '/').");

}

// Obtain filename from path

String[] parts = path.split("/");

String filename = (parts.length > 1) ? parts[parts.length - 1] : null;

// Split filename to prexif and suffix (extension)

String prefix = "";

String suffix = null;

if (filename != null) {

parts = filename.split("\\.", 2);

prefix = parts[0];

suffix = (parts.length > 1) ? "."+parts[parts.length - 1] : null; // Thanks, davs! :-)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值