簡體   English   中英

Java和JNI-加載dll庫僅在我的計算機上有效

[英]Java and JNI - Loading dll libraries only works on my computer

我要通過JNI使我的Java應用程序與外部c ++ dll一起工作時會遇到一些煩人的問題。 這些庫位於包內。 當我需要將它們加載到虛擬機時,將它們復制到一個臨時文件夾中。 dll是下一個:

libcurld.dll; libfftw3f-3.dll; libmad.dll; libsamplerate.dll; main.dll;

main.dll是實現在Java端聲明的本機方法的一個。 此dll取決於以上內容才能正常運行。 我只在Visual Studio上編譯main.ddl,一個用於7的二進制文件用於xp。 其他的已下載並只需鏈接。 我運行下一個方法以在Java上加載庫:

public static boolean loadBinaries(){
    String os = System.getProperty("os.name").toLowerCase();
    ArrayList<String> bins = new ArrayList<String>();

    if(os.indexOf("windows 7") >= 0){
        bins.add("/nm/metadata/bin/win/libcurld.dll");
        bins.add("/nm/metadata/bin/win/libfftw3f-3.dll");
        bins.add("/nm/metadata/bin/win/libmad.dll");
        bins.add("/nm/metadata/bin/win/libsamplerate.dll");
        bins.add("/nm/metadata/bin/win/seven/main.dll");
    }
    else if(os.indexOf("windows xp") >= 0){
        bins.add("/nm/metadata/bin/win/libcurld.dll");
        bins.add("/nm/metadata/bin/win/libfftw3f-3.dll");
        bins.add("/nm/metadata/bin/win/libmad.dll");
        bins.add("/nm/metadata/bin/win/libsamplerate.dll");
        bins.add("/nm/metadata/bin/win/xp/main.dll");
    }

    File f = null;
    for(String bin : bins){
        InputStream in = FileManager.class.getResourceAsStream(bin);
        byte[] buffer = new byte[1024];
        int read = -1;
        try {
            String[] temp = bin.split("/");
            f = new File(TEMP_FOLDER + "/" + temp[temp.length-1]);
            File realF = new File(f.getAbsolutePath());
            if(realF.exists())
                f.delete();

            FileOutputStream fos = new FileOutputStream(realF);

            while((read = in.read(buffer)) != -1) {
                fos.write(buffer, 0, read);
            }
            fos.close();
            in.close();

            System.load(f.getAbsolutePath());
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    return true;
}

可執行jar在我的機器上始終可以完美運行,但在其他機器上卻無法正常工作...我從測試中得到了下一個錯誤:

(xp)
Exception in thread "main" java.lang.UnsatisfiedLinkError: C:\temp\main.dll: Can't find dependent libraries at java.lang.ClassLoader$NativeLibrary.load(Native Method)...

(seven)
C:\temp\main.dll: The application has failed to start because its side-by-side configuration is incorrect...

所有的dll都寫在temp文件夾中,正如我所說,這在我的計算機上運行良好。 我以為這可能是因為在VS上以調試模式進行編譯。 不幸的是,除非返回較小的二進制文件,否則將其釋放不會改變任何事情。

這會是什么? Visual Studio是否缺少一些配置詳細信息? 提前致謝。

首先,我將復制所有DLL,然后在兩個循環中加載它們。

其次,從錯誤消息看來,main.dll要求還有另一個在其他計算機上不存在的DLL。 可能是C ++運行時庫經常在您所需的版本中未安裝,這就是為什么許多游戲或其他應用程序首先安裝那些原因的原因。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM