0

I am new at Java ME platform. I am creating a demo MIDlet poject (IMLETDemo4) at Eclipse Juno 4.2. The project should include eclipse.paho.client.mqtt jar in order to send GPIO pins values to mqtt subscribers. So I installed eclipse.paho.client.mqtt jar and import to the project. While writing code, no compile error exist. But when I run the demo project, classes from eclipse.paho.client.mqtt jar cannot found.Why occures such NoClassDefFoundError ? What sould I do to solve?

    import javax.microedition.midlet.MIDlet;
    import javax.microedition.midlet.MIDletStateChangeException;

    import org.eclipse.paho.client.mqttv3.MqttClient;
    import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
    import org.eclipse.paho.client.mqttv3.MqttMessage;
    import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

    import com.oracle.deviceaccess.PeripheralConfig;
    import com.oracle.deviceaccess.PeripheralManager;
    import com.oracle.deviceaccess.gpio.GPIOPin;
    import com.oracle.deviceaccess.gpio.GPIOPinConfig;

    public class ControlGpioExample extends MIDlet {

    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
        // TODO Auto-generated method stub

    }

    protected void pauseApp() {
        // TODO Auto-generated method stub

    }

    protected void startApp() throws MIDletStateChangeException {
        // TODO Auto-generated method stub
        System.out.println("GPIO pin test...");

        GPIOPinConfig fwdConf = new GPIOPinConfig(0, 7,
                GPIOPinConfig.DIR_OUTPUT_ONLY, PeripheralConfig.DEFAULT,
                GPIOPinConfig.TRIGGER_NONE, false);

        GPIOPinConfig backConf = new GPIOPinConfig(0, 8,
                GPIOPinConfig.DIR_OUTPUT_ONLY, PeripheralConfig.DEFAULT,
                GPIOPinConfig.TRIGGER_NONE, false);

        GPIOPinConfig inputConf = new GPIOPinConfig(0, 11,
                GPIOPinConfig.DIR_INPUT_ONLY, PeripheralConfig.DEFAULT,
                GPIOPinConfig.TRIGGER_NONE, false);

        GPIOPin pin7 = null;
        GPIOPin pin8 = null;
        GPIOPin pin11 = null;

        testMe();
        try {

            pin7 = (GPIOPin) PeripheralManager.open(fwdConf);
            pin8 = (GPIOPin) PeripheralManager.open(backConf);
            pin11 = (GPIOPin) PeripheralManager.open(inputConf);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            System.out.println("Error accessing gpio pins");
            e.printStackTrace();
        }

        if (pin7 == null || pin8 == null || pin11 == null) {
            System.out.println("GPIO pin is null! Unable to run the program.");
        } else {
            try {

                for (int i = 0; i < 5;) {
                    boolean stateChange = false;
                    boolean pin11Val = false;
                    stateChange = pin11Val == pin11.getValue() ? false : true;

                    System.out.println("Motor iteration " + i);
                    System.out.println("-----------------------");

                    if (stateChange) {
                        Thread.sleep(1000);
                        pin7.setValue(true);
                        pin8.setValue(true);
                        System.out.println("pin7: " + toString(pin7.getValue())
                                + " --- pin8: " + toString(pin8.getValue())
                                + " --- pin11: " + toString(pin11.getValue()));

                        **/*LOOK THIS METHOD*/**
                        mqttSpeaker(toString(pin7.getValue()), toString(pin8.getValue()), toString(pin11.getValue()));
                        pin11Val = pin11.getValue();

                        i++;
                    } else {
                        Thread.sleep(1000);
                        pin7.setValue(false);
                        pin8.setValue(false);
                        System.out.println("pin7: " + toString(pin7.getValue())
                                + " --- pin8: " + toString(pin8.getValue())
                                + " --- pin11: " + toString(pin11.getValue()));
                    }

                    Thread.sleep(1000);

                }

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    }

    public String toString(boolean value) {
        return value ? "true" : "false";
    }
    public void mqttSpeaker(String pin1, String pin2, String pin3) {
        String topic = "home"; // "MQTT Examples";
        String content;
        content = "Message from MqttPublish ::: ";
        int qos = 1;// 2;
        String broker = "tcp://localhost:1883"; // "tcp://iot.eclipse.org:1883";
        String clientId = "JavaSample";

        try {
            content = content + " pin7:  "+pin1+ "; pin8:  "+ pin2 + "; pin11:  "+ pin3;

        } catch (Exception e) {
            e.printStackTrace();
        }

        MemoryPersistence persistence = new MemoryPersistence();

        try {
            MqttClient sampleClient = new MqttClient(broker, clientId,
                    persistence);
            MqttConnectOptions connOpts = new MqttConnectOptions();
            connOpts.setCleanSession(true);
            System.out.println("Connecting to broker: " + broker);
            sampleClient.connect(connOpts);
            System.out.println("Connected");
            System.out.println("Publishing message: " + content);
            MqttMessage message = new MqttMessage(content.getBytes());
            message.setQos(qos);
            sampleClient.publish(topic, message);
            System.out.println("Message published");
            sampleClient.disconnect();
            System.out.println("Disconnected");
            System.exit(0);
        } catch (Exception me) {
            me.printStackTrace();
        }
}
//    
    public static void testMe()
    {
        System.out.println("Test Success");
    }

}

Ecipse Console log:

Installing suite from: file:///C:/Users/omertasci/workspaceforME/IMLETDemo4/.mtj.tmp/emulation/IMLETDemo4.jad
 java.lang.NoClassDefFoundError: org/eclipse/paho/client/mqttv3/persist/MemoryPersistence
 - java.lang.Class.invoke_verify(), bci=0
 - java.lang.Class.initialize(), bci=98
 - java.lang.Class.forName(), bci=0
 - com.sun.midp.main.CldcMIDletLoader.newInstance(), bci=1
 - com.sun.midp.midlet.MIDletStateHandler.createMIDlet(), bci=64
 - com.sun.midp.midlet.MIDletStateHandler.createAndRegisterMIDlet(), bci=17
 - com.sun.midp.midlet.MIDletStateHandler.startSuite(), bci=25
 - com.sun.midp.main.AbstractMIDletSuiteLoader.startSuite(), bci=38
 - com.sun.midp.main.CldcMIDletSuiteLoader.startSuite(), bci=5
 - com.sun.midp.main.AbstractMIDletSuiteLoader.runMIDletSuite(), bci=124
 - com.sun.midp.main.AppIsolateMIDletSuiteLoader.main(), bci=26
[AMS-TRACE] MIDlet:IMLETDemo4 MIDlet Suite status=2
[AMS-TRACE] MIDlet:IMLETDemo4 MIDlet Suite status=3
[AMS-CRITICAL] MIDlet:IMLETDemo4 MIDlet Suite abnormal exit
javacall_lifecycle_state_changed() lifecycle: event is     JAVACALL_LIFECYCLE_MIDLET_SHUTDOWN status is JAVACALL_OK
ÖMER TAŞCI
  • 546
  • 5
  • 9
  • I'm assuming you added mqtt jar to build path. Did you also export the mqtt jar? – Telmo Pimentel Mota Mar 31 '17 at 14:35
  • Thanks @Telmo Pimentel Mota 2 . I think, I found the problem. My demo project must be run on an emulator. (I created the emulator at Eclipse from "Custom Device Editor"). When I run the demo, an emulator is displayed and my project is tried to load on emulator. At this point, my project contains mqtt jar, so the emulator does not accept mqtt jar because it cannot be located in system files of emulator. Hence, some security constraints block me. Now, I think so. – ÖMER TAŞCI Apr 03 '17 at 13:26
  • NoClassDefFoundError means MemoryPersistence is not present in IMLETDemo4.jar. Somewhere in your build process mqtt.jar must be unpacked, added to your projects classes and packed in IMLETDemo4.jar. – Telmo Pimentel Mota Apr 04 '17 at 11:12

0 Answers0