Java and native access to USB ports

Java and native access to USB ports

This week, we publish a new version of the Java library. This new version comes with a new feature: native USB access. From now on, you can access the USB ports without running the VirtualHub.

Thanks to the new version of our Java library, you can access Yoctopuce modules connected to the machine running the Java code without using the VirtualHub. Use of the USB modules is the same as with C++, C#, Objective-C, Delphi, VB-Net, and Python languages.

To access the USB ports, you must simply replace your call to YAPI.RegisterHub("localhost") by a call to YAPI.RegisterHub("usb").

try {

  // sets up the API to use direct USB access

  System.out.println("Device connected by USB");
  YModule module = YModule.FirstModule();
  while (module != null) {
    module = module.nextModule();

} catch (YAPI_Exception ex) {
  System.out.println("error:" + ex.getLocalizedMessage());

There is however a little trick...

Originally, the Java Virtual Machine (JVM) can't reach the machine USB ports. To work around this limitation, we used Java Native Interface (JNI) to load a dynamic library written in C.

Therefore, our new library is made of two parts:

  • The Java code containing the objects of our library
  • A yapi dynamic library for each operating system (Windows 32bits, Windows 64 bits, Linux 32bits, ...)

You can directly copy the Java code into you project and compile it, but you can also add the YoctoLib.jar file to your classpath.

However, you must install the dynamic library on the machine running the application. To do so, you must copy the yapi library in a directory listed in the java.library.path property of the JVM. This property is simply a list of directories in which the JVM looks for the dynamic libraries. By default, this property is initialized as follows:

under Windowswith the environment variable PATH
under Linuxwith the environment variable LD_LIBRARY_PATH
under OS Xwith the environment variable DYLD_LIBRARY_PATH

If you want to use other directories, you can modify this property in the application code with the following piece of code:

  System.setProperty("java.library.path", "c:\example\yoctopuce\library");

But you can also specify this property when running from a command line with the "-D" option.

$ java -Djava.library.path=c:\example\yoctopuce\library MainClass

In the opposite to the Java code which works on any OS, each library is made of C code compiled to work on a very specific OS. This explains why we have several binaries for this library. You can either install only the correct version, or all the files, the code is smart enough to use the correct one.

The list of yapi dynamic library versions:

Windows 32 bitsyapi.dll
Windows 64 bitsyapi64.dll
Linux 32
Linux 64
Linux ARM 64
Mac OS Xlibyapi.dylib

The library archive contains only the already compiled binaries, but you can very well recompile them by downloading the C++ library.

When the application is running, the JVM starts by loading the Java code and by running it, without taking the dynamic library into account. It's only when there is the call to YAPI.RegisterHub("usb") that it searches all the directories of the "java.library.path" property to find the yapi dynamic library. If no compatible library is found, an exception is raised. Otherwise, the JVM loads the library and execution goes on.

The Java part of our library is designed to work with or without the yapi dynamic library. Obviously, if the dynamic library is not there, you cannot access the USB ports directly with the call to YAPI.RegisterHub("usb"), but you can still use the VirtualHub or connect your devices to a YoctoHub.

You can use the Java library with a YoctoHub, with the VirtualHub, or with the USB ports of the machine
You can use the Java library with a YoctoHub, with the VirtualHub, or with the USB ports of the machine

Here you are, you can now directly access the modules that are connected to the USB ports of your machine.

1 - mischu Saturday,october 17,2015 15H42

In eclipse just go to "Java Build Path" -> "Libraries" and select the yocotoAPI and click on the subnote "native library location". So you don't need add code. Just add the native library to your project...

Yoctopuce, get your stuff connected.