How does JNA load multiple dependent DLL libraries?
The emergence of JNA greatly simplifies the original JNI technology. Below is the JNA github address: https://github.com/java-native-access/jna
1. A simple example:
/** Simple example of JNA interface mapping and usage. */public class HelloWorld { public interface CLibrary extends Library { CLibrary INSTANCE = (CLibrary)Native.loadLibrary((Platform.isWindows() ? "msvcrt" : "c"), CLibrary.class); void printf(String format, Object... args);// void printf(const char *format, [argument]); } public static void main(String[] args) { CLibrary.INSTANCE.printf("Hello, World\n"); for (int i=0; i < args.length; i++) { CLibrary.INSTANCE.printf("Argument %d: %s\n", i, args[i]); } } }
The principle is to use CLibrary extends Libarary to load the system DLL/so library and list the DLL library functions to be called by Java, the ing between the function and the DLL library function is completed by JNA.
In this way, you can call the functions in the DLL/so library.
2. How to Use JNA to load multiple DLL libraries and there is dependency between them
Http://stackoverflow.com/questions/32609829/load-multiple-libraries-with-jna
Is there a way in JNA to load multiple libraries with Java?
I usually useNative.loadLibrary(...)To load one DLL. But I guess its not working this way because I assign this function call to the instance member.
Let's say I have libraryfooAnd librarybar.barHas a dependency onfoo; It also has a dependency onbaz, Which we areNotMapping with JNA:
public class Foo { public static final boolean LOADED; static { Native.register("foo"); LOADED = true; } public static native void call_foo();}public class Bar { static { // Reference "Foo" so that it is loaded first if (Foo.LOADED) { System.loadLibrary("baz"); // Or System.load("/path/to/libbaz.so") Native.register("bar"); } } public static native void call_bar();}
The callSystem.load/loadLibraryWill only be necessary ifbazIs neither on your library load path (PATH/LD_LIBRARY_PATH, For windows/linux respectively) nor in the same directorybar(Windows only ).
EDIT
You can also do this via interface mapping:
public interface Foo extends Library { Foo INSTANCE = (Foo)Native.loadLibrary("foo");}public interface Bar extends Library { // Reference Foo prior to instantiating Bar, just be sure // to reference the Foo class prior to creating the Bar instance Foo FOO = Foo.INSTANCE; Bar INSTANCE = (Bar)Native.loadLibrary("bar");}
-----------------
3. Example code:
Public class DDLTest {public static void main (String [] args) {System. out. println (System. getProperty ("java. library. path "); FingerLibrary. fingerdll. ZAZCloseDeviceEx (-1); int r = Response (); ID_FprCap.fprCap.LIVESCAN_Close (); response (1); Response (1, "aaaaaa"); r = Response ("aaaaaaaaaaa ", "aaaaaaaaaaa", (float) 0.5);} public interface FingerLibrary extends Library {int FPDATASIZE = 256; int IMGWIDTH = 256; int IMGHEIGHT = 288; int IMGSIZE = 73728; // window message int WM_FPMESSAGE = 1024 + 120; // custom message int FPM_DEVICE = 0x01; // The status prompt int FPM_PLACE = 0x02; // press the finger int FPM_LIFT = 0x03; // lift the finger int FPM_CAPTURE = 0x04; // capture the image int FPM_ENROLL = 0x06; // register the fingerprint template int FPM_GENCHAR = 0x05; // The collection feature int FPM_NEWIMAGE = 0x07; // the fingerprint image int RET_ OK = 0x1; int RET_FAIL = 0x0; FingerLibrary Fingerdll = (FingerLibrary) Native. loadLibrary ("ZAZAPIt", FingerLibrary. class); public int ZAZOpenDeviceEx (int [] hHandle, int nDeviceType, int iCom, int iBaud, int nPackageSize, int iDevNum); public int ZAZCloseDeviceEx (int handle ); public int ZAZVfyPwd (int pHandle, int nAddr, byte [] pPassword); public int ZAZReadInfPage (int pHandle, int nAddr, byte [] pInf); public int ZAZReadIndexTable (int pHandle, int nAddr, int nPage, byte [] UserContent); // public int FP_FeatureMatch (String pFeatureData1, String pFeatureData2, float pfSimilarity );} public interface ID_Fpr extends Library {ID_Fpr INSTANCE = (ID_Fpr) Native. loadLibrary ("ID_Fpr", ID_Fpr.class); // public int LIVESCAN_Init (); public int FP_FeatureMatch (String pFeatureData1, String pFeatureData2, float pfSimilarity ); // int _ stdcall FP_FeatureMatch (unsigned char * pFeatureData1, // input parameter fingerprint feature data 1 // unsigned char * pFeatureData2, // input parameter fingerprint feature data 2 // float * pfSimilarity); // output parameter matching similarity value 0.00-1.00 // typedef int (_ stdcall * FP_FeatureMatch) (unsigned char * pFeatureData1, // input parameter fingerprint feature data 1 // unsigned char * pFeatureData2, // input parameter fingerprint feature data 2 // float * pfSimilarity ); // output parameter matching similarity value 0.00-1.00} public interface ID_FprCap extends StdCallLibrary {ID_Fpr fpr = ID_Fpr.INSTANCE; ID_FprCap fprCap = (ID_FprCap) Native. loadLibrary ("ID_FprCap", ID_FprCap.class); public int LIVESCAN_Init (); // int _ stdcall LIVESCAN_Init (); public int LIVESCAN_Close (); // int _ stdcallLIVESCAN_Close (); public int LIVESCAN_BeginCapture (int nChannel); public int LIVESCAN_GetFPRawData (int nChannel, String pRawData); // int _ stdcallLIVESCAN_BeginCapture (int nChannel); // int _ encode (int nChannel, unsigned char * pRawData); public int livescan_getfpbmp data (int nChannel, String pBmpData); // int _ stdcalllivescan_getfpbmp data (int nChannel, unsigned char * pBmpData ); public int LIVESCAN_EndCapture (int nChannel); // int _ stdcallLIVESCAN_EndCapture (int nChannel); public int livescan_issuppsetup setup (); // int _ login (); public int LIVESCAN_GetVersion (); // int _ stdcallLIVESCAN_GetVersion (); public int LIVESCAN_GetDesc (String pszDesc); // int _ expect (char pszDesc [1024]); public int LIVESCAN_GetErrorInfo (int nErrorNo, string pszErrorInfo); // int _ stdcallLIVESCAN_GetErrorInfo (int nErrorNo, char pszErrorInfo [256]);}