Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I place both the 32bit and 64bit versions of a native library on java.library.path?

Is it possible to place two versions (64-bit and 32-bit) of a native DLL on the java.library.path?

I'm concerned at whether if I placed the 64-bit and the 32-bit versions on the native library path, there would be an unsatisfied link exception in running for a different architecture. If so, what is the preferred solution? to have a flag in my gradle/maven build script that links the correct library?

I'm currently doing the following: -

-Djava.library.path=/out/lib/win64jdk;/out/lib/winx86jdk

Both containing the relevant DLLs for the architecture. I'm not currently sure whether or not the JVM can determine the symbols.

like image 966
chrisw Avatar asked Oct 01 '22 00:10

chrisw


1 Answers

If I were you I'd ensure that only the appropriate native dll was in the distributable package and leave the other one out.

If however you want to distribute both then you need to write some Java code to load the correct library. System.loadLibrary is used to load a native library and the normal thing to do is to call this in a static initialiser in your Java code.

Unless you're willing to rely crudely on exceptions, you can use this code:

System.getProperty("sun.arch.data.model") 

to load the appropriate native library. This function will return either 32 or 64 accordingly.

like image 73
Bathsheba Avatar answered Oct 03 '22 01:10

Bathsheba