Hamid Fadishei
Hamid Fadishei

Reputation: 868

JNI - Package both 64-bit and 32-bit DLLs for Windows

I use JNI in one of my Java applications and I want to package both 64-bit and 32-bit DLLs in the product for my Windows users. Is there a way (probably a naming convention) to follow so that System.loadLibrary("foo") automatically knows which file should be loaded? I currently use this workaround which I do not like:

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

Upvotes: 0

Views: 1094

Answers (1)

qwr
qwr

Reputation: 3670

If you want to know if 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;
    }

and then

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

        } catch (UnsatisfiedLinkError ex) {

        }
    }

Upvotes: 1

Related Questions