2017-09-19 75 views
0

我想在我的Raspberry Pi 3上使用XBee Java Lib及其教程做一个简单的Xbee示例,但我想在将它转换为.jar文件之前执行它。我只想将它作为一个.class文件来执行,非常简单,之后我想将它导入到另一个项目中。 (我不擅长与Java,因为它是可以看到) 编译我试图执行它之后:在树莓派上使用Java Lib的XBee

java -cp $XBJL_CLASS_PATH:. com.digi.xbee.example.MainApp 

echo $XBJL_CLASS_PATH是:

libs/xbee-java-library-1.2.1.jar:libs/rxtx-2.2.jar:libs/slf4j-api-1.7.12.jar:libs/slf4j-nop-1.7.12.jar:libs/android-sdk-5.1.1.jar:libs/android-sdk-addon-3.jar 

这意味着所有的.jar缴费至从XBee Java Lib中使用。

它没有work.I've也尝试过:

java com.digi.xbee.example.MainApp 

我总是得到同样的错误:

Error: A JNI error has occurred, please check your installation and try again 
Exception in thread "main" java.lang.NoClassDefFoundError: com/digi/xbee/api/XBeeDevice 
     at java.lang.Class.getDeclaredMethods0(Native Method) 
     at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) 
     at java.lang.Class.privateGetMethodRecursive(Class.java:3048) 
     at java.lang.Class.getMethod0(Class.java:3018) 
     at java.lang.Class.getMethod(Class.java:1784) 
     at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544) 
     at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526) 
Caused by: java.lang.ClassNotFoundException: com.digi.xbee.api.XBeeDevice 
     at java.net.URLClassLoader.findClass(URLClassLoader.java:381) 
     at java.lang.ClassLoader.loadClass(ClassLoader.java:424) 
     at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) 
     at java.lang.ClassLoader.loadClass(ClassLoader.java:357) 
     ... 7 more 

有谁知道什么可以happenning?这是说我没有导入XBeeDevice,我导入了libs/xbee-java-library-1.2.1.jar

PS:代码启动与此:

package com.digi.xbee.example; 

    import com.digi.xbee.api.WiFiDevice; 
    import com.digi.xbee.api.XBeeDevice; 
    import com.digi.xbee.api.exceptions.XBeeException; 
    import com.digi.xbee.api.models.XBeeProtocol; 

public class MainApp { 
    /* Constants */ 
    // TODO Replace with the port where your sender module is connected to. 
    private static final String PORT = "/dev/ttyAMA0/"; 
    // TODO Replace with the baud rate of your sender module. 
    private static final int BAUD_RATE = 9600; 

    private static final String DATA_TO_SEND = "Hello XBee World!"; 

    public static void main(String[] args) { 
     XBeeDevice myDevice = new XBeeDevice(PORT, BAUD_RATE); 
     byte[] dataToSend = DATA_TO_SEND.getBytes(); 

     try { 
      myDevice.open(); 

      System.out.format("Sending broadcast data: '%s'", new String(dataToSend)); 

      if (myDevice.getXBeeProtocol() == XBeeProtocol.XBEE_WIFI) { 
       myDevice.close(); 
       myDevice = new WiFiDevice(PORT, BAUD_RATE); 
       myDevice.open(); 
       ((WiFiDevice)myDevice).sendBroadcastIPData(0x2616, dataToSend); 
      } else 
       myDevice.sendBroadcastData(dataToSend); 

      System.out.println(" >> Success"); 

     } catch (Exception e) { 
      System.out.println(" >> Error"); 
      e.printStackTrace(); 
      System.exit(1); 
     } 
     finally { 
      myDevice.close(); 
     } 
    } 
} 

在此先感谢。

回答

0

我已经完成了。 :D 我不得不在我的CLASSPATH绝对库路径,如/home/pi/.../libs/xbee-java-library-1.2.1.jar:...

之后,我有另一个错误,说有关RXTX lib。为了打通这一块,我需要做的sudo apt-get install librxtx-java因为在这里 java.library.path location 说,像这样运行:

java -Djava.library.path=/usr/lib/jni -cp $XBJL_CLASS_PATH:. com.digi.xbee.example.MainApp

希望它可以帮助别人。