Eclipse 运行Java ME MIDlet时出现NoClassDefFoundError

Eclipse 运行Java ME MIDlet时出现NoClassDefFoundError,eclipse,java-me,noclassdeffounderror,midlet,Eclipse,Java Me,Noclassdeffounderror,Midlet,我是JavaME平台的新手。我正在EclipseJuno4.2上创建一个演示MIDlet项目(IMLETDemo4)。项目应该包括eclipse.paho.client.mqtt jar,以便将GPIO引脚值发送给mqtt订户。因此,我安装了eclipse.paho.client.mqtt jar并导入到项目中。编写代码时,不存在编译错误。但是当我运行演示项目时,找不到eclipse.paho.client.mqtt jar中的类。为什么会发生这样的NoClassDefFoundError?我该

我是JavaME平台的新手。我正在EclipseJuno4.2上创建一个演示MIDlet项目(IMLETDemo4)。项目应该包括eclipse.paho.client.mqtt jar,以便将GPIO引脚值发送给mqtt订户。因此,我安装了eclipse.paho.client.mqtt jar并导入到项目中。编写代码时,不存在编译错误。但是当我运行演示项目时,找不到eclipse.paho.client.mqtt jar中的类。为什么会发生这样的NoClassDefFoundError?我该怎么解决呢

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

}

我假设您添加了mqtt jar来构建路径。您还导出了mqtt jar吗?谢谢@Telmo Pimentel Mota 2。我想,我发现了问题所在。我的演示项目必须在模拟器上运行。(我在Eclipse上从“自定义设备编辑器”创建了模拟器)。当我运行演示时,会显示一个模拟器,并尝试在模拟器上加载我的项目。此时,我的项目包含mqtt jar,因此emulator不接受mqtt jar,因为它不能位于emulator的系统文件中。因此,一些安全限制阻止了我。NoClassDefFoundError意味着IMLETDemo4.jar中不存在MemoryPersistence。必须在构建过程中的某个地方解包mqtt.jar,将其添加到项目类中,并打包到IMLETDemo4.jar中。
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