title: How to programmatically load librxtxSerial in Java
body: <p>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 : <a href="http://blog.iharder.net/2009/08/18/rxtx-java-6-and-librxtxserial-jnilib-on-intel-mac-os-x/" rel="nofollow">http://blog.iharder.net/2009/08/18/rxtx-java-6-and-librxtxserial-jnilib-on-intel-mac-os-x/</a> as recommended here : <a href="http://arduino.cc/playground/Interfacing/Java" rel="nofollow">http://arduino.cc/playground/Interfacing/Java</a></p>
  
  <p>After that I tried to load it programmatically like this :</p>
  
  <pre><code>/**
    * 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();
      }
  }
  </code></pre>
  
  <p>Which seems to works (at least does not throw an exception).</p>
  
  <p>But when I call the <code>CommPortIdentifier.getPortIdentifier(PORT_NAME);</code> it throws the following exception:</p>
  
  <pre><code>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.&lt;clinit&gt;(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)
  </code></pre>
  
  <p>So I do not understand what's wrong, since it seems to be the right way to load it according to : <a href="http://docs.oracle.com/javase/6/docs/api/java/lang/System.html#load%28java.lang.String%29" rel="nofollow">http://docs.oracle.com/javase/6/docs/api/java/lang/System.html#load(java.lang.String)</a></p>
  
  <p>Any idea?</p>
  
  <p>PS : You can find the whole code here : <a href="https://gist.github.com/1853637" rel="nofollow">https://gist.github.com/1853637</a> which works provided the rxtx jar is in the classpath and the native library location is defined (like this in Eclipse : <a href="http://www.eclipsezone.com/eclipse/forums/t49342.html" rel="nofollow">http://www.eclipsezone.com/eclipse/forums/t49342.html</a>)</p>