NoClassDefFondError in Android... but the Class is in one jar included in the Classpath
Asked Answered
Y

6

18

I am developing an App in Android. It has to be able of take a photo, and to send that photo to a webpage. This is the code:

HttpClient httpclient = new DefaultHttpClient();

        HttpPost httpPost = new HttpPost("someurl");
        MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); //Here throws the exception
        multipartEntity.addPart("data",
                new InputStreamBody(
                        new ByteArrayInputStream(byteArray),
                        "image/png"));
        multipartEntity.addPart("caption", new StringBody(filename));

        httpPost.setEntity((HttpEntity) multipartEntity);

I have the libraries needed in my ClassPath (httpclient, apache-mime4j-core, httpcore and httpmime). It doesn't shows any error in compilation time. but, when i run the project, it says " java.lang.NoClassDefFoundError: org.apache.http.entity.mime.MultipartEntity" but that class DOES exists in the jars (in httpMime, exactly). Here is the full trace:

04-09 10:21:59.362: E/AndroidRuntime(10352): FATAL EXCEPTION: main
04-09 10:21:59.362: E/AndroidRuntime(10352): java.lang.NoClassDefFoundError: org.apache.http.entity.mime.MultipartEntity
04-09 10:21:59.362: E/AndroidRuntime(10352):    at     com.publidirecta.AppAzafata.IniciarGPSActivity2.enviarImagen(IniciarGPSActivity2.java:206)
04-09 10:21:59.362: E/AndroidRuntime(10352):    at com.publidirecta.AppAzafata.IniciarGPSActivity2.onActivityResult(IniciarGPSActivity2.java:196)
04-09 10:21:59.362: E/AndroidRuntime(10352):    at android.app.Activity.dispatchActivityResult(Activity.java:3908)
04-09 10:21:59.362: E/AndroidRuntime(10352):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2549)
04-09 10:21:59.362: E/AndroidRuntime(10352):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:2595)
04-09 10:21:59.362: E/AndroidRuntime(10352):    at android.app.ActivityThread.access$2000(ActivityThread.java:121)
04-09 10:21:59.362: E/AndroidRuntime(10352):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:973)
04-09 10:21:59.362: E/AndroidRuntime(10352):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-09 10:21:59.362: E/AndroidRuntime(10352):    at android.os.Looper.loop(Looper.java:130)
04-09 10:21:59.362: E/AndroidRuntime(10352):    at  android.app.ActivityThread.main(ActivityThread.java:3701)
04-09 10:21:59.362: E/AndroidRuntime(10352):    at java.lang.reflect.Method.invokeNative(Native Method)
04-09 10:21:59.362: E/AndroidRuntime(10352):    at java.lang.reflect.Method.invoke(Method.java:507)
04-09 10:21:59.362: E/AndroidRuntime(10352):    at     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
04-09 10:21:59.362: E/AndroidRuntime(10352):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
04-09 10:21:59.362: E/AndroidRuntime(10352):    at dalvik.system.NativeStart.main(Native Method)

I've tried with older versions of all the jars used in this task, but it still doesn't works. That jars do appear in the "referenced Libraries" in the Android project.

I've tried everything. Anybody has any idea why this happens? im about to throw myself for the window.

Thank you in advance!

Yellowtail answered 9/4, 2012 at 8:37 Comment(3)
Create another new project, and copy these classes into that and try again...Large
see this answer that may help you | https://mcmap.net/q/627664/-android-update-17-seems-incompatible-with-external-jarsTouchhole
I just had this, Eclipse let me reference the file but when it ran, error! I just recomplied and it worked tho ....Caernarvonshire
S
29

you need create a folder named libs in your project, and copy you referenced jars to this folder.

Showoff answered 9/4, 2012 at 8:41 Comment(2)
And don't forget Project > Clean after putting it into libs folder.Spada
i did the same also but i'm getting always java.lang.NoClassDefFoundError: org.apache.http.entity.mime.HttpMultipartFlannel
G
9

Being unsuccessful at the automatic updating thing, I downloaded the latest and greatest. But the MultipartEntity class couldn't be found. I re-added it to the project. I made sure it was in a folder called 'libs' - but no luck. Andong's prompted me to check the project preferences. Project Properties -> Java Build Path -> Order and Export: make sure that Android Private Libraries is checked. (I checked them all). Clean your project. The problem is gone.

Gamber answered 5/6, 2013 at 0:39 Comment(1)
In my case it was just the httpmime that had to be checked in "Order and Export"; after a project cleanup, the problem went away!Faustinafaustine
R
3

So weird, my app already had the files in "lib" and got this crash. I renamed it "libs" following idiottiger's response and it worked.

Regretful answered 2/8, 2012 at 10:35 Comment(1)
Same result. Thanks idiottiger and thank you Eclipse for wasting another two hours of my life!Trypsin
P
3

Open Properties for your project in Eclipse. And open Order and Export, make sure all libraries you need are selected. And click Project>clean. Finally, your problem should be gone.

Pappus answered 21/5, 2013 at 15:18 Comment(0)
L
2

I had a few issues with this today:

a) I had all the jar files to the libs directory and went right click on Project > Build Path > Add external jars > selected the jar files. Then went "order and export", still got the issue. b) Turns out that httpclient-4.3.1.jar and httpmime-4.3.1.jar weren't enough. I needed to add httpcore-4.3.jar as well.

That got rid of the error! :D I have spent nearly 4 hours today trying to fix this :(

Louielouis answered 19/1, 2014 at 9:43 Comment(0)
O
0

I solved the same issue like this:

  1. Open pom.xml in the project you are facing this
    java.lang.NoClassDefFoundError: org/apache/http/entity/mime/MultipartEntityBuilder

  2. Under dependencies, add this piece of code.

      <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>4.5.2</version>
            <scope>test</scope>
        </dependency>
    
  3. Clean and rebuild the code.

It will work.

Orgeat answered 9/11, 2016 at 10:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.