Compiling Javahidapi for the Raspberry Pi

This document tracks the steps required to compile the javahidapi library on Raspberry Pi. Javahidapi is a JNI-based wrapper for the HID API libraries; it allows Java programs to access USB HID class devices. We needed it in order for our Java API to work with the Finch robot and the Hummingbird Robotics Kit, but there many other uses as well.

Here are the steps taken. If you want to skip these steps, the final jar and native library files are attached to the page (compiled with source current as of January 2013):

  1. Install the default Raspbian wheezy OS (with hard floating point) on an SD card and plug it into the Raspberry Pi. We used the excellent Berryboot installer for our installation.
  2. Configure the OS and make sure to enable ssh so you can connect remotely. The default username is "pi" with password "raspberry".
  3. Update the default packages by typing sudo apt-get update followed by sudo apt-get upgrade. Note that each of these commands may take 10 minutes to several hours. 
  4. Once you've updated, download Oracle's Java JDK 8 for ARM. Tip: It's probably easier to download it to a host computer and then transfer it to the Raspberry Pi.
  5. Install the Java JDK following these instructions. The instructions are for Oracle JDK7, which did not work on the default wheezy OS. JDK8 does work on the default OS, so ignore the "Prerequisites" section of these instructions. 
  6. Once Java is installed, install ant from a binary. Do not use sudo apt-get install for this, instead follow the "Short Story" instructions on the site to install from a tarball.
  7. Make sure that you have set your environment variables correctly. Type nano ~/.bashrc and ensure that the following lines are in the file (you may need to modify JAVA_HOME and ANT_HOME based on where you placed these programs):
    	JAVA_HOME=/opt/jdk1.8.0
    	export JAVA_HOME
    	ANT_HOME=/home/pi/apache-ant-1.8.4
    	export ANT_HOME
    	PATH=$JAVA_HOME/bin:$ANT_HOME/bin:$PATH
    	export PATH
  8. Now checkout the source code for javahidapi from its googlecode repository. Make sure you get the full source including the native libraries, and not just the "hidapi-1.1-sources.jar" file.
  9. Copy all javahidapi source code to the Raspberry Pi from the host computer; we placed it in ~/javahidapi.
  10. Now make the native C library to generate the libhidapi-jni.so file needed by the Java code:
    1. If you don't want to do steps b-h, my compiled file is attached to the page.
    2. Install libusb1.0: sudo apt-get install libusb-1.0-0-dev
    3. Install libudev: sudo apt-get install libudev-dev
    4. Navigate to the linux directory in the javahidapi source code.
    5. Modify the Makefile by adding the following line; for convenience, the modified Makefile is attached to this page. Note that you may need to modify this line based on where you installed the JDK:
      JAVA8HEADERS=-I/opt/jdk1.8.0/include -I/opt/jdk1.8.0/include/linux
    6. Now modify the JNIINCLUDES line to add the JAVA8HEADERS
      JNIINCLUDES=-I.. -I../jni-impl $(JAVA5HEADERS) $(JAVA6HEADERS) $(JAVA7HEADERS) $(JAVA8HEADERS)
    7. Type make all
    8. Congratulations, you've just built libhidapi-jni.so; this file is now found in the "linux" directory.  
  11. Now to build the full Javahidapi library. Go to the root directory of the javahidapi code and replace the build.xml file with the one attached to this page. This file has been modified to specify the java 1.8 compiler. The modifications are at lines 64-66. Make sure to change the following line to point to the install location for your JDK: 
    executable="/opt/jdk1.8.0/bin/javac"
  12. Copy the native library you've just created into lib/native/linux (note the 32 added to the filename): cp linux/libhidapi-jni.so lib/native/linux/libhidapi-jni-32.so
     
  13. In the top-level javahidapi directory, type ant dist. With any luck everything will compile and three jar files will appear in dist/lib, including hidapi-1.1.jar. Warning: this jar is unlikely to work on non-arm Linux computers, and may not work on Windows or Mac either. 
  14. Eat a cookie, then write some java programs to control HID devices.