Java DLL Communication Error

I am using libusb-- http://sourceforge.net/apps/trac/libusb-win32/wiki

However, I get:

Exception in thread "main" java.lang.UnsatisfiedLinkError: USBManager.usb_init () in

public class USBManager { static{ System.loadLibrary("libusb"); } native void usb_init(); public USBManager() { usb_init(); } } 
+4
source share
5 answers

You cannot just use the public usb_init (); and then load such a native library, JNI is not implemented that way.

you use javah to create an .h file that you can use to create a library that implements certain native functions in the class.

 javac USBManager 

Creates a class file that you use with javah:

 javah USBManager 

This gives a file in this place with the name "USBManager.h", which indicates the functions to be implemented in .so / .dll that implement the corresponding native function.

 /* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class USBManager */ #ifndef _Included_USBManager #define _Included_USBManager #ifdef __cplusplus extern "C" { #endif /* * Class: USBManager * Method: usb_init * Signature: ()V */ JNIEXPORT void JNICALL Java_USBManager_usb_1init (JNIEnv *, jobject); #ifdef __cplusplus } #endif #endif 

So, you need to export a function called "Java_USBManager_usb_1init" that takes the specified parameters.

This function can be no more than:

 JNIEXPORT void JNICALL Java_USBManager_usb_1init (JNIEnv *, jobject) { usb_init(); } 

There is a pretty good simple example of a Sun developer blog , but there are many other examples there.

+2
source

There is a Java wrapper for this library that is already written. Why don't you give it a try?

+3
source

Try System.loadLibrary("usb");

0
source

Either usb.dll cannot be found, try System.load () using the abbsolute path instead of System.loadLibrary () to check this.

Another problem may be that libusb relies on other DLLs. Use Dependency Walker to see which DLLs are referenced by libusb.

Another problem may be that the DLL does not export the function using the Corrent signature. The DLL must have a USBManager_usb_init () function. Use javah to create the correct signature.

0
source

JNI is pretty minimalistic, any function that jni accesses requires a built-in shell function written against your class. The javah tool generates a header containing the required wrappers.

It’s easy to use JNA to access native features.

0
source

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


All Articles