0

I started to play with arduino an interfacing it with Java, it was pretty straightforward, but now I would like to programmatically load the librxtxSerial native lib, and I can't figure out. Since I'm on MacOS 64bit and use Java6 I used the following jnilib : http://blog.iharder.net/2009/08/18/rxtx-java-6-and-librxtxserial-jnilib-on-intel-mac-os-x/ as recommended here : http://arduino.cc/playground/Interfacing/Java

After that I tried to load it programmatically like this :

/**
  * Loads the jnilib
  */
public static void loadJniLib() {
    // loads the jnilib from the source folder "src/main/resources"
    URL url = Demo.class.getResource("/librxtxSerial.jnilib");
    try {
        System.load(url.getPath());
    }
    catch (UnsatisfiedLinkError unsatisfiedLinkError) {
        // native code library failed to load.
        unsatisfiedLinkError.printStackTrace();
    }
}

Which seems to works (at least does not throw an exception).

But when I call the CommPortIdentifier.getPortIdentifier(PORT_NAME); it throws the following exception:

java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path thrown while loading gnu.io.RXTXCommDriver
Exception in thread "main" java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1758)
    at java.lang.Runtime.loadLibrary0(Runtime.java:823)
    at java.lang.System.loadLibrary(System.java:1045)
    at gnu.io.CommPortIdentifier.<clinit>(CommPortIdentifier.java:83)
    at fr.free.mdwhatever.arduino.maven.Demo.initialize(Demo.java:57)
    at fr.free.mdwhatever.arduino.maven.Demo.main(Demo.java:102)

So I do not understand what's wrong, since it seems to be the right way to load it according to : http://docs.oracle.com/javase/6/docs/api/java/lang/System.html#load(java.lang.String)

Any idea?

PS : You can find the whole code here : https://gist.github.com/1853637 which works provided the rxtx jar is in the classpath and the native library location is defined (like this in Eclipse : http://www.eclipsezone.com/eclipse/forums/t49342.html)

JoshDM
  • 4,939
  • 7
  • 43
  • 72
xavier.seignard
  • 11,254
  • 13
  • 51
  • 75

1 Answers1

0

The simplest way to work with this is to set the java.library.path system property, for example on the commandline using -Djava.library.path=/usr/lib/jni.

As I recall, library loading is designed to fail if the library is not explicitly whitelisted. That, in turn, means that the library must be on the java.library.path.

user268396
  • 11,576
  • 2
  • 31
  • 26
  • Thanks for your comment, I also tried to programmatically modify the `java.library.path` by adding the folder containing my jnilib and calling `System.loadLibrary("librxtxSerial");` with no luck either. Still, I know the other possibilities I have, but I would like to do it programmatically. – xavier.seignard Feb 17 '12 at 16:02