0

I want to create an JSONObject in my Servlet:

import org.json.JSONObject;
[...]    
Map<String, String> data1 = new HashMap<String, String>();
Map<String, String> data2 = new HashMap<String, String>();
data1.put("name", "f1");
data1.put("key", "aa1");
data1.put("value", "21");
data2.put("name", "f2");
data2.put("key", "aa2");
data2.put("value", "22");

JSONObject json1 = new JSONObject(data1);

But when I run my application, I get the exception:

exception 
javax.servlet.ServletException: Servlet execution threw an exception    

root cause 
java.lang.NoClassDefFoundError: org/json/JSONObject
    servlet.LoginServlet.doPost(LoginServlet.java:71)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:643)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:723)


root cause 
java.lang.ClassNotFoundException: org.json.JSONObject
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
    servlet.LoginServlet.doPost(LoginServlet.java:71)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:643)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:723)

I have used the following json resource: https://code.google.com/p/org-json-java/downloads/list

1 Answer 1

1

Download java-json jar file and add it into class path.

For Classpath reference: Right click on project in eclipse -> Buildpath -> Configure Build path -> Java Build Path (left Pane) -> Libraries(Tab) -> Add External Jars -> Select your jar and select ok.

Sign up to request clarification or add additional context in comments.

8 Comments

So the one I got from code.google.com/p/org-json-java/downloads/list is not the right one?
Did you add this in classpath?
Yes I had added in Java Build Path --> Libraries --> Add External Jars, but still no same problem
Can you try with this jar file java2s.com/Code/Jar/j/Downloadjavajsonjar.htm?
Yeah that one I just tried, but still ClassNotFoundException... :/
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.