JNI - 为Windows打包64位和32位DLL

时间:2013-12-10 08:43:43

标签: java java-native-interface

我在我的一个Java应用程序中使用JNI,我想在我的Windows用户的产品中打包64位和32位DLL。是否有一种方法(可能是命名约定),以便System.loadLibrary("foo")自动知道应该加载哪个文件?我目前使用这种我不喜欢的解决方法:

// try to load foo64.dll and if faild, load foo32.dll
try
{
   System.loadLibrary("foo64");
}
catch (UnsatisfiedLinkError e)
{
   System.loadLibrary("foo32");
}

1 个答案:

答案 0 :(得分:1)

如果你想知道Vm 64bit

public static boolean is64BitVM() { 
        String bits = System.getProperty("sun.arch.data.model", "?");
        if (bits.equals("64")) {
            return true;
        }
        if (bits.equals("?")) {
            return System.getProperty("java.vm.name")
                    .toLowerCase().indexOf("64") >= 0;

        }
        return false;
    }

然后

static {
        fail = false;
        try { 
                if (!is64BitVM())  
                    System.loadLibrary("foo32"); //  
                else 
                    System.loadLibrary("foo64"); //               

        } catch (UnsatisfiedLinkError ex) {

        }
    }