javaraspberry-pi3xbee

XBee using the Java Lib on Raspberry Pi


I'm trying to make a simple Xbee example on my Raspberry Pi 3 work, using the XBee Java Lib and its tutorial, but I want to execute it before transforming it to a .jar file. I want just to execute it as a .class file, very simple, after that I want to import it to another project. (I'm not good with Java, as it's possible to see) After compiling I tried to execute it as:

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

my echo $XBJL_CLASS_PATH is:

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

Which means all .jar avaiable to use from the XBee Java Lib.

It didn't work.I've also tried just:

java com.digi.xbee.example.MainApp

And I'm always getting the same error:

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

Does anybody know what could be happenning? It's saying I haven't imported the XBeeDevice, which I did, importing the libs/xbee-java-library-1.2.1.jar.

PS: The code starts with this:

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

Thanks in advance.


Solution

  • I've made it through. :D I had to put in my CLASSPATH the absolute libs path, like /home/pi/.../libs/xbee-java-library-1.2.1.jar:...

    After that I had another error, saying about the RXTX lib. To get through that one, I need to do an sudo apt-get install librxtx-java as it said here java.library.path location and run it like this:

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

    Hope it helps someone.