org/json/JSONException not found

dionisio_79
dionisio_79 used Ask the Experts™
on
Hi Experts,
can anyone tell me how can I solve this:
I get a classnotfound exception:
java.lang.NoClassDefFoundError: org/json/JSONException.
 My json package contains the JSONException class in net.sf.json package.
I replaced all my org.json references inside my source with net.sf.json but still got this exception.
Maybe I have some references inside other libraries. How can I find them and how can I fix the problem?
Thanks a lot
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016

Commented:
You need to set both compile and run time classpaths to include the json jars
Awarded 2011
Awarded 2011

Commented:

I'm using json-lib-2,4-jdk15.jar
from:

http://sourceforge.net/projects/json-lib/files/json-lib/json-lib-2.4/json-lib-2.4-jdk15.jar/download

and had no problems.

I didn't reference JSONException explicitly though in my code - but do you really need to do it?
Just catch(Exception ) if you need.

Thsi is an example of my code with JSON:

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

public class CreateJSONArray {

    public CreateJSONArray(){
        HashMap m1 = new HashMap();
        HashMap m2 = new HashMap();
        m1.put("1","a");
        m1.put("2","b");
        m2.put("3","c");
        m2.put("4","d");
        ArrayList al = new ArrayList();
        al.add(m1);
        al.add(m2);

        JSONArray   ja = new JSONArray();

        for(int j=0; j<al.size(); j++){
            HashMap m = (HashMap) al.get(j);
            Set s = m.keySet();
            Iterator it = s.iterator();
            JSONObject jo = new JSONObject();
            while(it.hasNext()){
                String s1 = (String)it.next();
                String val = (String) m.get(s1);
                jo.put(s1,val);
            }
            ja.add(jo);


        }
        JSONObject jo1 = new JSONObject();
        jo1.put("hashmaps", ja);

        System.out.println(jo1.toString());




        
    }

    public static void main(String [] args){
        new CreateJSONArray();
    }

}

Open in new window

Author

Commented:
Hi, thanks for answers,

I've got my jar in the runtime classpath and I added the jar as a library so it is in the compiling classpath as well
 I'm using the same version of the library (2.4). The code which is raising the exception is in the facebook api library (facebook-java-api-3.0.2

Awarded 2011
Awarded 2011
Commented:
Perhaps the code from facebook library relies on different json package.
You may try to use this one (and of couse change your references to net.sf... to org.json....):

http://mirrors.ibiblio.org/pub/mirrors/maven2/com/hp/hpl/jena/json-jena/1.0/json-jena-1.0.jar

Top Expert 2016

Commented:
>>The code which is raising the exception is in the facebook api library (facebook-java-api-3.0.2

I wish you'd said that earlier ;) You need to use the json library that ships with the facebook api

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial