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)