Can I host the 32-bit and 64-bit versions of the native library on java.library.path?

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

I'm worried if I put the 64-bit and 32-bit versions on the way to the source library, there would be an unsatisfied link exception when working 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 am currently doing the following: -

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

Both contain the appropriate DLLs for the architecture. I'm not sure if the JVM can detect characters.

+5
source share
2 answers

If I were you, I would make sure that the distribution package had only the corresponding native dll and leave another.

If you want to distribute both, you need to write Java code to load the correct library. System.loadLibrary used to load its own library, and the usual thing to do is call this in the static initializer in your Java code.

If you don't want to be rude to exceptions, you can use this code:

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

to download the appropriate library. This function will return 32 or 64, respectively.

+2
source

You can not add any of them, and then, before you need them (for example, when starting the application), download the corresponding file based on the current system architecture.

You can load it using System.loadLibrary() :

 boolean is64bit = ...; // See below how to detect System.loadLibrary("/out/lib/win" + (is64bit ? "64jdk" : "x86jdk")); 

Architecture Detection:

How do I know if I work in a 64-bit JVM or 32-bit JVM (from within the program)?

+1
source

Source: https://habr.com/ru/post/1201317/


All Articles