0

i am new to MQTT and paho Libs for Android. today I tried to create a simple connect example, as shown below, but what happens is that, I get error messages from logcat posted below despite there is no error in the code but at run time, either eclipse says can not run the projects due to errors "which are not exist" or the App crash and i got the below posted error logs.

Also, i posted below a pic of the libs i have in my project, which are downloaded from paho website.

please let me know what i am missing and what i did wrong.

code:

public class MQTT_00 extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mqtt_00_layout);

        MqttClientPersistence persistence = null;
        try {
            persistence = new MqttDefaultFilePersistence("/temp/");
        } catch (MqttPersistenceException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            MqttClient client = new MqttClient("tcp://localhost:1883", "MQTTSub", persistence);
            MqttConnectOptions opts = new MqttConnectOptions();
            opts.setKeepAliveInterval(480);
            opts.setWill(client.getTopic("willTopic"), "something bad happened".getBytes(), 1, true);
            client.connect(opts);
        } catch (MqttException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
}

}

LogCat:

11-10 12:52:49.617: E/AndroidRuntime(30700): FATAL EXCEPTION: main
11-10 12:52:49.617: E/AndroidRuntime(30700): Process: com.example.mqtt_00, PID: 30700
11-10 12:52:49.617: E/AndroidRuntime(30700): java.lang.NoClassDefFoundError:  
org.eclipse.paho.client.mqttv3.MqttDefaultFilePersistence
11-1 12:52:49.617: E/AndroidRuntime(30700):     at 
com.example.mqtt_00.MQTT_00.onCreate(MQTT_00.java:21)
11-10 12:52:49.617: E/AndroidRuntime(30700):    at   
android.app.Activity.performCreate(Activity.java:5389)
11-10 12:52:49.617: E/AndroidRuntime(30700):    at  
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
11-10 12:52:49.617: E/AndroidRuntime(30700):    at 
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2246)
11-10 12:52:49.617: E/AndroidRuntime(30700):    at 
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2340)
 11-10 12:52:49.617: E/AndroidRuntime(30700):   at 
android.app.ActivityThread.access$800(ActivityThread.java:157)
11-10 12:52:49.617: E/AndroidRuntime(30700):    at 
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
11-10 12:52:49.617: E/AndroidRuntime(30700):    at 
android.os.Handler.dispatchMessage(Handler.java:102)
11-10 12:52:49.617: E/AndroidRuntime(30700):    at android.os.Looper.loop(Looper.java:157)
11-10 12:52:49.617: E/AndroidRuntime(30700):    at 
android.app.ActivityThread.main(ActivityThread.java:5293)
11-10 12:52:49.617: E/AndroidRuntime(30700):    at java.lang.reflect.Method.invokeNative(Native 
Method)
11-10 12:52:49.617: E/AndroidRuntime(30700):    at 
java.lang.reflect.Method.invoke(Method.java:515)
11-10 12:52:49.617: E/AndroidRuntime(30700):    at   
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
11-10 12:52:49.617: E/AndroidRuntime(30700):    at c   
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
11-10 12:52:49.617: E/AndroidRuntime(30700):    at dalvik.system.NativeStart.main(Native Method)

Pic enter image description here

1 Answer 1

0

Ensure you added the library correctly by adding external Jar in Java build path-> libraries and followed by checking the library in order and export tab. Once done, clean the project and try to run again. this should solve the issue!

It worked at my end.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.