我是Java ME平台的新手。我正在Eclipse Juno 4.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");
}
}
Ecipse控制台日志:
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