0

Why does my app keep crashing after it shows up on the Emulator? I just started learning about Jsoup parsing and following different tuts, but I can't find solution to this error.

package com.example.testingjson;

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;


public class MainActivity extends Activity {

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

         Document doc;
            try {
                doc = Jsoup.connect("http://google.com/").get();
                String title = doc.title();
             System.out.print(title);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            }




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;

    }

}

And this is the Error Message

12-26 19:30:42.043: E/AndroidRuntime(4260): FATAL EXCEPTION: main
12-26 19:30:42.043: E/AndroidRuntime(4260): java.lang.NoClassDefFoundError: org.jsoup.Jsoup
12-26 19:30:42.043: E/AndroidRuntime(4260):     at com.example.testingjson.MainActivity.onCreate(MainActivity.java:22)
12-26 19:30:42.043: E/AndroidRuntime(4260):     at android.app.Activity.performCreate(Activity.java:5104)
12-26 19:30:42.043: E/AndroidRuntime(4260):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-26 19:30:42.043: E/AndroidRuntime(4260):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-26 19:30:42.043: E/AndroidRuntime(4260):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-26 19:30:42.043: E/AndroidRuntime(4260):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-26 19:30:42.043: E/AndroidRuntime(4260):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-26 19:30:42.043: E/AndroidRuntime(4260):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-26 19:30:42.043: E/AndroidRuntime(4260):     at android.os.Looper.loop(Looper.java:137)
12-26 19:30:42.043: E/AndroidRuntime(4260):     at android.app.ActivityThread.main(ActivityThread.java:5039)
12-26 19:30:42.043: E/AndroidRuntime(4260):     at java.lang.reflect.Method.invokeNative(Native Method)
12-26 19:30:42.043: E/AndroidRuntime(4260):     at java.lang.reflect.Method.invoke(Method.java:511)
12-26 19:30:42.043: E/AndroidRuntime(4260):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-26 19:30:42.043: E/AndroidRuntime(4260):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-26 19:30:42.043: E/AndroidRuntime(4260):     at dalvik.system.NativeStart.main(Native Method)

Edit: Here is a pic showing the JSOUP jar file imported, but I still get the error. enter image description here

APK file opened with Winrar.

enter image description here

Order & Export' settings

enter image description here

Questions
  • 303
  • 5
  • 16
  • `java.lang.NoClassDefFoundError: org.jsoup.Jsoup`: seems pretty self-explaining to me? The mentioned class is missing in the runtime classpath, while it was available in the compiletime classpath. – BalusC Jan 08 '13 at 23:48
  • So to fix it I must add the jsoup-1.7.1.jar library to the compile time classpath. How? – Questions Jan 09 '13 at 00:02
  • 1
    It's not missing in the compiletime classpath (otherwise you wouldn't be able to compile it). It's missing in runtime classpath. That's exactly what `NoClassDefFoundError` means. The class was successfully compiled with Jsoup dependency, but it is in turn missing during runtime. – BalusC Jan 09 '13 at 00:04
  • Where is this runtime classpath? I am not following you. I added a pic above. – Questions Jan 09 '13 at 00:12

2 Answers2

0

You need to import JSOUP jar file to your project. Easiest way is to put it in libs folder if you are on eclipse and it should add it automatically on build, or you can just do right click on project, build path -> Add external archives. Sometimes I also had problems importing certain jars to project but it was all resolved by building proj few times or restarting eclipse.

v0d1ch
  • 2,738
  • 1
  • 22
  • 27
0

Could you open up your apk & check if jsoup lib is present in your libs folder in the apk? If it isn't, that means it hasn't been added in the build. Next step is to do a clean, make sure that the previously built apk is deleted, copy jsoup to libs folder & rebuild the apk. Check if jsoup has been copied over to the newly created apk.

wraith
  • 391
  • 1
  • 14
  • Same problem. I created a new project, moved the jsoup jar to libs folder. Reformed clean up and launched the emulator and it crashed. Does the code work with you? – Questions Jan 09 '13 at 00:54
  • Did you check if your apk has the jsoup lib? – wraith Jan 09 '13 at 00:57
  • Which file is that? Is it the one inside the bin folder ? and do I open it with eclipse? Sorry still new. – Questions Jan 09 '13 at 01:01
  • Also, instead of having the jar on the Desktop, could you copy it to your project? – wraith Jan 09 '13 at 01:01
  • apk is your final application package that gets deployed to the device. It should be in the bin folder. Check for .apk extension. You can open it using 7-zip or winrar – wraith Jan 09 '13 at 01:03
  • Do you want me to provide pictures to specific locations? Maybe I am not following you right! – Questions Jan 09 '13 at 01:04
  • I opened the file with Winrar and it looks it's not there. I added more pics. – Questions Jan 09 '13 at 01:10
  • 1
    Try this .. Looks like the same problem that you are facing. http://stackoverflow.com/questions/9934744/android-java-lang-noclassdeffounderror-org-jsoup-jsoup – wraith Jan 09 '13 at 01:15
  • Sorry I don't see the jsoup library there, I fixed the pics. – Questions Jan 09 '13 at 01:15
  • 1
    As seen when you open up your apk, the jsoup lib is not present in your apk. So it is not being copied over during the build process. Try to follow the steps in the link that I pasted above. Copy jsoup to libs folder in your project & fix your 'Order & Export' settings. – wraith Jan 09 '13 at 01:23
  • Where do they talk about how to fix 'Order & Export' settings? I don't see it on the link. – Questions Jan 09 '13 at 01:30
  • I am still looping around the last step, not understanding it very well. I don't want to bother you a lot, but I added a new pic can you look at it. If you feel the answer is in that link and I should read it again and again, I will check your answer as the solution and look at it later. Thanks! – Questions Jan 09 '13 at 01:57