裝載本地庫

來源:互聯網
上載者:User

裝載本地庫
(lithium的成果,純粹作為資料儲存)
搞過JNI的都知道,本地庫要放到系統path中,這樣,Java進程在運行中才能找到本地庫並動態載入。我們可以通過環境變數System.getProperty("java.library.path")來查看當前JVM搜尋本地庫的路徑。

這時,就會遇到一個問題,部署應用的時候要記住將本地庫拷貝到環境變數path指定的路徑中。一般在windows平台上直接copy到 C:/WINDOWS/System32目錄下了事。但要換一台機器部署怎麼辦?除了要把Java程式拿過去,還要記的把本地庫也copy到正確的目錄, 真麻煩。於是想看看有什麼好辦法來解決這個問題。

首先,最容易想到的是,把本地庫和class檔案放在一起,利用Class.getResource(str)找到路徑,然後加到環境java.library.path中:
java代碼:

URL url = Foo.class.getResource("Foo.class");
String path = (new File(url.getPath())).getParent();
System.setProperty("java.library.path", path);

看上去很好,但卻不能工作。查了一下ClassLoader的原始碼,原來它把搜尋路徑定義為靜態變數並只初始化一次,後面再設定java.library.path就沒有用了。ClassLoader代碼片斷:
java代碼:

// The paths searched for libraries
static private String usr_paths[];
static private String sys_paths[];
...
if (sys_paths == null) {
        usr_paths = initializePath("java.library.path");
        sys_paths = initializePath("sun.boot.library.path");
}

正在一籌莫展是,翻看JACOB的原始碼,忽然有了驚喜的發現。
java代碼:

try
{
        //Finds a stream to the dll. Change path/class if necessary
    InputStream inputStream = getClass().getResource("/jacob.dll").openStream();
    //Change name if necessary
    File temporaryDll = File.createTempFile("jacob", ".dll");
    FileOutputStream outputStream = new FileOutputStream(temporaryDll);
    byte[] array = new byte[8192];
    for (int i = inputStream.read(array); i != -1; i = inputStream.read(array)) {
                outputStream.write(array, 0, i);
        }
    outputStream.close();
    temporaryDll.deleteOnExit();
    System.load(temporaryDll.getPath());
    return true;
}
catch(Throwable e)
{
        e.printStackTrace();
    return false;
}

高,真是好辦法。和我一樣,把dll放在classpath中,用Class.getResource(str).openStream()讀取這 個dll,然後寫到temp目錄中,最後用System.load(path)來動態載入。多說一句,為什麼得到了jacob.dll的URL不直接去加 載呢?想想看,如果把dll和class一起打成Jar包,ClassLoader還是不能載入本地庫,因為System.load(path)需要的是 dll的完整路徑,但並不支援jar協議。還不明白,看看下面的代碼:
java代碼:

URL url = Foo.class.getResource("/java/lang/String.class");
System.out.println(url.toExternalForm());
System.out.println(url.getFile());

在我的機器上的運行結果是:
java代碼:

jar:file:/D:/jdk1.5.0_06/jre/lib/rt.jar!/java/lang/String.class
file:/D:/jdk1.5.0_06/jre/lib/rt.jar!/java/lang/String.class

ClassLoader中用new File(name),當然會找不到檔案。同時,看看我的第一種方法,就算能設定成功環境java.library.path,如果dll是在jar包中,還是載入不了。

到現在,你是不是覺得問題已經解決了?還沒呢!jacob的很多源檔案中已經寫了下面的代碼:
java代碼:

static {
        System.loadLibrary("jacob");
}

除非我去掉這一句重新編譯jacob的原始碼,否則系統還是會報錯。不過,既然有了上面的想法,稍微變通一下,就可以巧妙的解決。首先找到環境java.library.path,然後把dll拷貝到其中一個路徑中就行了。
java代碼:

static {
        try {
                String libpath = System.getProperty("java.library.path");
                if ( libpath==null || libpath.length() == 0 ) {
                        throw new RuntimeException("java.library.path is null");
                }
                       
                String path = null;
                StringTokenizer st = new StringTokenizer(libpath, System.getProperty("path.separator"));
                if ( st.hasMoreElements() ) {
                        path = st.nextToken();
                } else {
                        throw new RuntimeException("can not split library path:" + libpath);
                }
                       
                InputStream inputStream = Foo.class.getResource("jacob.dll").openStream();
                final File dllFile = new File(new File(path), "jacob.dll");
                if (!dllFile.exists()) {
                        FileOutputStream outputStream = new FileOutputStream(dllFile);
                        byte[] array = new byte[8192];
                        for (int i = inputStream.read(array); i != -1; i = inputStream.read(array)) {
                                outputStream.write(array, 0, i);
                        }
                        outputStream.close();
                }
                //dllFile.deleteOnExit();       
                Runtime.getRuntime().addShutdownHook(new Thread(){
                        public void run() {
                                if ( dllFile.exists() ) {
                                        boolean delete = dllFile.delete();
                                        System.out.println("delete : " + delete);
                                }
                        }
                });
                } catch (Throwable e) {
                        throw new RuntimeException("load jacob.dll error!", e);
        }
}

唯一的美中不足,在系統關閉的時候刪除dll總是不能成功,試了兩種辦法都不行。想想也對,dll正被程式使用,當然不能刪除。翻了一下API,Java好像沒用提供unload本地庫的功能,只好做罷。

自己在項目中的代碼:
package jp.co.nec.necst.conference.util;
import java.io.*;
import java.util.*;
public class RegistryUtil {

static {
        try {
                String libpath = System.getProperty("java.library.path");
                if ( libpath==null || libpath.length() == 0 ) {
                        throw new RuntimeException("java.library.path is null");
                }
                       
                String path = null;
                StringTokenizer st = new StringTokenizer(libpath, System.getProperty("path.separator"));
                if ( st.hasMoreElements() ) {
                        path = st.nextToken();
                } else {
                        throw new RuntimeException("can not split library path:" + libpath);
                }
                Class thisClass = RegistryUtil.class;
                InputStream inputStream = thisClass.getResource("RegistryUtil.dll").openStream();
                File dllFile = new File(new File(path), "RegistryUtil.dll");
                if (!dllFile.exists()) {
                        FileOutputStream outputStream = new FileOutputStream(dllFile);
                        byte[] array = new byte[8192];
                        for (int i = inputStream.read(array); i != -1; i = inputStream.read(array)) {
                                outputStream.write(array, 0, i);
                        }
                        outputStream.close();
                       // System.loadLibrary("RegistryUtil");
                }
               
                System.loadLibrary("RegistryUtil");
              
                } catch (UnsatisfiedLinkError e) {
                    throw e;
                }catch (Throwable e) {
                        throw new RuntimeException("load RegistryUtil.dll error!", e);
        }
}

public static native String getRegValue(String regDir, String regName);
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.