[java] java.lang.NoClassDefFoundError: org/json/JSONObject

I am using Eclipse IDE and am writing a servlet. The servlet should accept values from an html file and return JSON response accordingly.

My doPost() is:

protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

        try
        {
        res.setContentType("application/json");
        res.setHeader("Cache-Control", "nocache");
            res.setCharacterEncoding("utf-8");

        PrintWriter out = res.getWriter();

        JSONObject json = new JSONObject();

        String un=req.getParameter("uname");
        String pw=req.getParameter("password");

        if(un.equals("xxx") && pw.equals("xxx"))            
            json.put("result", "success");
        else
            json.put("result", "fail");

        out.print(json.toString());
        }
        catch(Exception e){System.out.print( e.getMessage());}  
    }

When I run this servlet in Eclipse I get a file download dialog.

When run outside Eclipse with Tomcat, I get error:

root cause

java.lang.NoClassDefFoundError: org/json/JSONObject
    Server1.doPost(Server1.java:25)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

root cause

java.lang.ClassNotFoundException: org.json.JSONObject
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1713)
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1558)
    Server1.doPost(Server1.java:25)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

The line Server1.doPost(Server1.java:25) refers to

JSONObject json = new JSONObject();

I have added the org.json.jar to the build path as well as added it to deployment path in Properties->Configure build path->Deployment assembly

This question is related to java json servlets

The answer is


No.. It is not proper way. Refer the steps,

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.

For Deployment Assembly: Right click on WAR in eclipse-> Buildpath -> Configure Build path -> Deployment Assembly (left Pane) -> Add -> External file system -> Add -> Select your jar -> Add -> Finish.

This is the proper way! Don't forget to remove environment variable. It is not required now.

Try this. Surely it will work. Try to use Maven, it will simplify you task.


Add json jar to your classpath

or use java -classpath json.jar ClassName

refer this


The Exception it self says it all java.lang.ClassNotFoundException: org.json.JSONObject

You have not added the necessary jar file which will be having org.json.JSONObject class to your classpath.

You can Download it From Here


Please add the following dependency http://mvnrepository.com/artifact/org.json/json/20080701

<dependency>
   <groupId>org.json</groupId>
   <artifactId>json</artifactId>
   <version>20080701</version>
</dependency>

Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to json

Use NSInteger as array index Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) HTTP POST with Json on Body - Flutter/Dart Importing json file in TypeScript json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 190) Angular 5 Service to read local .json file How to import JSON File into a TypeScript file? Use Async/Await with Axios in React.js Uncaught SyntaxError: Unexpected token u in JSON at position 0 how to remove json object key and value.?

Examples related to servlets

Google Recaptcha v3 example demo Difference between request.getSession() and request.getSession(true) init-param and context-param java.lang.NoClassDefFoundError: org/json/JSONObject how to fix Cannot call sendRedirect() after the response has been committed? getting error HTTP Status 405 - HTTP method GET is not supported by this URL but not used `get` ever? Create a simple Login page using eclipse and mysql Spring get current ApplicationContext insert data into database using servlet and jsp in eclipse What is WEB-INF used for in a Java EE web application?