简体   繁体   English

Java和JNI-加载dll库仅在我的计算机上有效

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

I'm facing some annoying problems to get my java application working with external c++ dlls, through JNI. 我要通过JNI使我的Java应用程序与外部c ++ dll一起工作时会遇到一些烦人的问题。 The libraries are located inside packages. 这些库位于包内。 I copy them to a temporary folder when I need to load them into virtual machine. 当我需要将它们加载到虚拟机时,将它们复制到一个临时文件夹中。 The dlls are the next: dll是下一个:

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

The main.dll is the one that implements the native method declared on the java side. main.dll是实现在Java端声明的本机方法的一个。 This dll depends on the above to run properly. 此dll取决于以上内容才能正常运行。 I only compiled the main.ddl on visual studio, one binary for 7 other for xp. 我只在Visual Studio上编译main.ddl,一个用于7的二进制文件用于xp。 The others were downloaded and simply linked. 其他的已下载并只需链接。 I run the next method to load the libraries on java: 我运行下一个方法以在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;
}

The executable jar works always perfectly on my machine, but not in others... I got the next errors from my tests: 可执行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...

All the dlls are well written in the temp folder and, as I said, this works well in my computer. 所有的dll都写在temp文件夹中,正如我所说,这在我的计算机上运行良好。 I had thought this could be because compiling in debug mode on VS. 我以为这可能是因为在VS上以调试模式进行编译。 Unfortunately, turning it to release didn't change anything, unless returning smaller binaries. 不幸的是,除非返回较小的二进制文件,否则将其释放不会改变任何事情。

What can this be? 这会是什么? Is some configuration detail missing on visual studio? Visual Studio是否缺少一些配置详细信息? Thanks in advance. 提前致谢。

First, I would copy all DLLs and then load them in two loops. 首先,我将复制所有DLL,然后在两个循环中加载它们。

Second, from the error message it seems that there is yet another DLL that main.dll requires that is not present on the other machines. 其次,从错误消息看来,main.dll要求还有另一个在其他计算机上不存在的DLL。 It might be the C++ runtime libraries what often not installed in the version you need, which is why many games or other apps install those first. 可能是C ++运行时库经常在您所需的版本中未安装,这就是为什么许多游戏或其他应用程序首先安装那些原因的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM