NoClassDefFoundError,而类路径中的.jar

时间:2012-11-27 18:35:02

标签: android

我正在尝试使用json-lib-2.4-jdk15.json。我的代码中没有任何错误,但是当我运行我的程序时,catlog:

Caused by: java.lang.NoClassDefFoundError: net.sf.json.JSONSerializer

我有“import net.sf.json.JSONSerializer;”和eclipse在这一行没有显示任何错误。 我添加了像这样的.jar:配置构建路径 - >添加外部jar ..(所以现在jar出现在Referenced Libraries中)

我的课程:

package com.example.androidgridweb;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;

import android.os.AsyncTask;

public class eSearchElastic{



public void ESE (final ImageAdapter imgAdapter)throws ClientProtocolException, IOException {

AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>(){



    @Override
    protected Void doInBackground(Void... params) {
        String server = "servename";
        String index = "images";
        String type = "images_schema_1";
        StringEntity query = null;
        try {
            query = new StringEntity("{\"sort\" : [ {\"confidence_level\" : {\"order\" : \"desc\"} }],\"from\" : 0, \"size\" : 10,\"query\" : {\"text_phrase\" : { \"keyword\" : \"finding nemo\"}},\"filter\" : {\"numeric_range\" : {\"confidence_level\" : { \"from\" : 10, \"to\" : 100, \"include_lower\" : true, \"include_upper\" : true}}}}'");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        int i=0;
        HttpClient httpclient = new DefaultHttpClient();
        StringBuilder urlStr = new StringBuilder("http://");
         urlStr.append(server)
                .append("/" + index)
                .append("/" + type + "/_search");
         System.out.println(urlStr.toString());
         while (i < 5) {
            HttpPost httpost = new HttpPost(urlStr.toString());
            httpost.setEntity((HttpEntity)query);

         httpost.setHeader("Accept", "application/json");
            httpost.setHeader("Content-type", "application/json");

             HttpResponse response = null;
            try {
                response = httpclient.execute(httpost);
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

             String answer = null;
            try {
                answer = org.apache.http.util.EntityUtils.toString(response.getEntity());
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println(answer);
            i++;

            JSONObject answerJson = (JSONObject) JSONSerializer.toJSON(answer);
            String urlImage;
            try {
                urlImage = answerJson.getString("hits");
                JSONObject answerJson1 = (JSONObject) JSONSerializer.toJSON(urlImage);
                String urlImage1 = answerJson1.getString("hits").replace("[", "").replace("]","");
                JSONObject answerJson2 = (JSONObject) JSONSerializer.toJSON(urlImage1);
                String urlImage2 = answerJson2.getString("_source");
                JSONObject answerJson3 = (JSONObject) JSONSerializer.toJSON(urlImage2);
                String urlImage3 = answerJson3.getString("url");
                System.out.println(urlImage3);
                imgAdapter.addmThumbIds(urlImage3);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

         }
        return null;
    }

};
task.execute();

}

}

目录下载:

FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
    at android.os.AsyncTask$3.done(AsyncTask.java:278)
    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
    at java.lang.Thread.run(Thread.java:864)
Caused by: java.lang.NoClassDefFoundError: net.sf.json.JSONSerializer
    at com.example.androidgridweb.eSearchElastic$1.doInBackground(eSearchElastic.java:82)
    at com.example.androidgridweb.eSearchElastic$1.doInBackground(eSearchElastic.java:1)
    at   android.os.AsyncTask$2.call(AsyncTask.java:264)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)

1 个答案:

答案 0 :(得分:4)

将jar添加到构建路径并不意味着jar将包含在应用程序apk中。这就是它在编译时(eclipse)工作的原因,并且它不在运行时。

在android app项目的顶层创建一个“libs”文件夹,然后将jar复制到该文件夹​​中。 ADT插件会自动将该文件夹中的任何jar添加到eclipse构建路径和构建的apk文件中。