2012-02-17 17 views
0

我開始使用Arduino與Java進行連接,這非常簡單,但現在我想以編程方式加載librxtxSerial本機庫,並且我弄不明白。 由於我在MacOS 64位和使用的Java6我用下面的jnilib:http://blog.iharder.net/2009/08/18/rxtx-java-6-and-librxtxserial-jnilib-on-intel-mac-os-x/爲在這裏推薦:http://arduino.cc/playground/Interfacing/Java如何以編程方式加載Java中的librxtxSerial

之後,我試圖以編程方式加載它是這樣的:

/** 
 * 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(); 
    } 
} 

這似乎作品(至少不會拋出異常)。

但是,當我打電話CommPortIdentifier.getPortIdentifier(PORT_NAME);它拋出以下異常:

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) 

所以我不明白什麼是錯的,因爲它似乎是正確的方式,根據加載它:http://docs.oracle.com/javase/6/docs/api/java/lang/System.html#load(java.lang.String)

任何想法?

PS:你可以找到整個代碼在這裏:https://gist.github.com/1853637其作品所提供的RXTX罐子在類路徑和本機庫的位置定義(這樣在Eclipse:http://www.eclipsezone.com/eclipse/forums/t49342.html

回答

0

一起工作的最簡單方法這是設置java.library.path系統屬性,例如在命令行上使用-Djava.library.path=/usr/lib/jni

我記得,如果圖書館沒有明確列入白名單,圖書館的加載就會失敗。這反過來又意味着圖書館必須在java.library.path上。

+0

感謝您的評論,我也嘗試通過添加包含我的jnilib的文件夾並調用'System.loadLibrary(「librxtxSerial」);'也沒有運氣來以編程方式修改'java.library.path'。 不過,我知道我有其他的可能性,但我想以編程方式做到這一點。 – 2012-02-17 16:02:34

相關問題