Java : switching between dll depends on system architecture (32/64)

不羁的心 提交于 2019-12-19 11:21:56

问题


I have a Java program uses some dlls. As these embeded dlls have to be built for a specific system architecture (32 or 64bits) I want to make a method/something that allow my program to switch between 32/64 bits version of dlls (or disable the library load if the program run on a 64 bits system)

I hope there is a solution different from making two versions of the program

Thanks in advance, Damien


回答1:


Use system properties:

if ("x86".equals(System.getProperty("os.arch"))) {
   // 32 bit
} else if ("x64".equals(System.getProperty("os.arch"))) {
   // 64 bit
}



回答2:


You can use the System Property sun.arch.data.model

String dataModel = System.getProperty("sun.arch.data.model");
if("32").equals(dataModel){
    // load 32-bit DLL
}else if("64").equals(dataModel){
    // load 64-bit DLL
}else{
    // error handling
}

Careful: this property is defined on Sun VMs only!

Reference:

  • Java HotSpot FAQ > When writing Java code, how do I distinguish between 32 and 64-bit operation?



回答3:


A brute force way is to run

boolean found = false;
for(String library: libraries)
    try {
        System.loadLibrary(library);
        found = true;
        break;
    } catch(UnsatisfiedLinkError ignored) {
    }
if(!found) throw new UnsatifiedLinkError("Could not load any of " + libraries);



回答4:


If you're using OSGi and JNI, you can specify the DLLs appropriate for different platforms and architectures in the manifest via Bundle-NativeCode.

For example:

    Bundle-NativeCode: libX.jnilib; osname=macOSX, X.dll;osname=win32;processor=x86



回答5:


Define a java interface that represents your DLL API and provide two implementations, one that calls the 32 bit DLL and another one that calls the 64 bit version:

public interface MyDll {
    public void myOperation();
}

public class My32BitDll implements MyDll {
    public void myOperation() {            
        // calls 32 bit DLL
    }
}

public class My64BitDll implements MyDll {
    public void myOperation() {            
        // calls 64 bit DLL
    }
}

public class Main {
    public static void main(String[] args) {
        MyDll myDll = null;

        if ("32".equals(args[0])) {
            myDll = new My32BitDll();
        } else if ("64".equals(args[0])) {
            myDll = new My64BitDll();
        } else {
            throw new IllegalArgumentException("Bad DLL version");
        }

        myDll.myOperation();
    }
}


来源:https://stackoverflow.com/questions/5379589/java-switching-between-dll-depends-on-system-architecture-32-64

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!