javahidapi is a convenient library for accessing HID devices in a platform-independent way.   Here is how to re-build the native libraries on Windows and re-package them into a new JAR.  These steps are required to apply the patch needed to allow multiple Windows applications to access the same HID device at the same time.

Setup Build Environment

To build on Windows, you will need:

Ensure the following environment variables are set:

  • JAVA_HOME (eg. set JAVA_HOME=C:\Program Files\Java\jdk1.7.0)
  • JAVA_HEADER (eg. set JAVA_HEADER=C:\Program Files\Java\jdk1.7.0\include)
  • PATH=%PATH%;C:\Program Files\Netbeans 7.2.1\java\ant\bin *or* to wherever you installed Ant

The JAVA_HEADER environment variable  adds JNI headers to the Include path in Visual Studio.  Apache Ant should be on your path, and JAVA_HOME should point to the JDK (not the JRE).  To set an environment variable:

Start -> Right Click Computer  -> Properties -> Advanced System Settings -> Advanced -> Environment Variables

You can verify the variables are set properly by typing “echo %VARIABLE_NAME%” at a command prompt.  Verify Ant is on your path by typing “ant” at the command prompt.

Get Source

Get the source from Google Code using your Mercurial client.  If installed, you can use the Hg context menus from TortoiseHg, or run the following from the command line:

hg clone https://code.google.com/p/javahidapi/

Build Windows Native Libraries

Open javahidapi\windows\hidapi.sln with Visual Studio.  Depending on your version of Visual Studio, you may need to convert the project first.

“Build” the solution.  If you encounter errors about a missing hidapi.lib, ensure the projects are built in this order:  hidapi, hidapi-jni, hidtest.  If you encounter errors about missing jni.h, verify JAVA_HEADER is set correctly, or update the project’s properties to correctly set the Additional Include Directories to the “include” directory with the JDK.

Test the native library by adding the VID and PID of an HID device attached to the system to line 69 of hidtest.cpp in the hidtest project.  Build and run, to ensure everything is working correctly.

If you are applying any patches (like the read/write sharing patch), now is the time!

Finally, build 32 and 64-bit Release versions of hidapi.dll.  Change the Solution configuration from Debug to Release and build twice:  once with the Win32 Solution Platform, and the second time with the x64 Solution Platform.  Copy the resulting .DLLs into the lib directory:

javahidapi> copy windows\Release\hidapi-jni.dll lib\native\win\hidapi-jni-32.dll
javahidapi> copy windows\x64\Release\hidapi-jni.dll lib\native\win\hidapi-jni-32.dll

Other Native Libraries

The mercurial source includes binary versions of the native libraries for Windows, Mac, and Linux, so the final JAR will still support these platforms even if you don’t rebuild their libraries.

If you have rebuilt the Mac or Linux native libraries, copy them into the javahidapi\lib\native directory, overwriting the ones provided with the source.

Linux (Ubuntu 12.04)

See README.txt at javahidpi/linux for more details.

hg clone https://code.google.com/p/javahidapi/
cd javahidapi
sudo apt-get install libusb-1.0 libudev-dev
make
cp libhidapi-jni.so ../lib/native/linux/libhidapi-jni-XX.so  #where XX is 32 or 64

Here’s a link showing how to build a 64-bit library on a 32-bit system, but I haven’t personally done this before.  You may need to modify the Makefile to the the correct paths to the JAVA[567]HEADERS.  If you are unsure what path to use, search for “jni.h” (eg. sudo updatedb; locate jni.h).

Build JAR

The last step is to compile the Java sources and package them, along with all the native libraries, into the final JAR.  While it is possible to create a new Netbeans Java Application from Existing Sources, it isn’t really necessary so long as you have the environment variables set correctly (see above).  Run the following from the command line:

javahidapi> ant dist

The resulting JAR can be found in javahidapi\dist\lib.

Download

Here are pre-built JARs that contain the read/write fix for Windows.

[wpdm_file id=11 title=”false” desc=”true” ]

 

Tagged with →  
Share →

5 Responses to How To Build javahidapi From Source

  1. Dan says:

    Hi,

    First, thanks for your time in putting together the jar files for javahidapi.

    I am using netbeans. I have spent the last three days trying to solve the following error: Exception in thread “AWT-EventQueue-0” java.lang.UnsatisfiedLinkError: USB_GUI.HIDManager.init()V

    I know several other people are also having the same problem since this error as an issue on the code.google.com/p/javahidapi/issues/list.

    I am using your hidapi-rw-1.1.jar and have placed it in the classpath for netbeans. I know that the native library is loading correctly, but I still keep getting the UnsatisfiedLinkError.

    Can you point me in the right direction to fix this issue?

    Thanks,

    Dan

    • abythell says:

      Perhaps my “hidtest” Netbeans project will help? You can find the project on GitHub. Have a look at the code, and see if you can build and run it successfully. I think the key is to load the native HID library like this:

      public class HidTest {
      static {
      ClassPathLibraryLoader.loadNativeHIDLibrary();
      }
      }

      Otherwise, without seeing your project code it’s rather difficult to diagnose…

  2. Dan says:

    I forgot to add in my comment that I am using Windows 7.

    Thanks,

    Dan

  3. Dan says:

    Thanks, I’ll build your hidTest and see if I can get it working.

    Dan

  4. Dan says:

    Hey,

    Calling ClassPathLibraryLoader.loadNativeHIDLibrary(); in that manner worked! I also imported the same libraries that you did in HidTest and that may have also helped to fix the problem. Thanks a lot!

    Dan

Leave a Reply

Your email address will not be published.