4

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()V

public class USBManager 
{   
    static{
        System.loadLibrary("libusb");   
    }

    native void usb_init();
    public USBManager()
    {       
        usb_init();     
    } 
}
Trufa
  • 39,971
  • 43
  • 126
  • 190

5 Answers5

3

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

Romain Hippeau
  • 24,113
  • 5
  • 60
  • 79
2

You can't just use public native usb_init(); and then load a native library like that, the JNI is not implemented that way.

you use javah to create a .h file the can be used to create a library that implements the specific native functions in the class.

javac USBManager

Creates a class file, that you use with javah:

javah USBManager

This yields a file in that location called 'USBManager.h', which specifies the functions to implement in a .so/.dll that implement the relevant 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 to parameters specified.

That function can be nothing more than:

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

There is a pretty good simple example on a blog by a Sun developer, but there are lots of other examples out there.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Anya Shenanigans
  • 91,618
  • 3
  • 107
  • 122
0

Try System.loadLibrary("usb");

Jochen Bedersdorfer
  • 4,093
  • 24
  • 26
0

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

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

Another problem might be, that the DLL does not export a function with the corrent Signature. There should be a USBManager_usb_init() function in the DLL. Use javah to create the correct signature.

Daniel
  • 27,718
  • 20
  • 89
  • 133
0

JNI is rather minimalistic, any function accessed by jni requires a native wrapper function written against your class. The tool javah generates a header containing the required wrappers.

To access native functions the easy way use JNA.

josefx
  • 15,506
  • 6
  • 38
  • 63