NoClassDefFondError in Android… but the Class is in one jar included in the Classpath

亡梦爱人 提交于 2019-12-18 12:57:10

问题


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!


回答1:


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




回答2:


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.




回答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.




回答4:


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.




回答5:


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 :(




回答6:


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.



来源:https://stackoverflow.com/questions/10070788/noclassdeffonderror-in-android-but-the-class-is-in-one-jar-included-in-the-cl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!