34

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

26
  • Hope the below links helps stackoverflow.com/questions/14657585/… Commented Dec 16, 2014 at 12:20
  • Just check whether your library (org.json.jar) is generated in the deployment location. As per your comments you have added the required jar in your classpath and deployment assembly So it should be generated in the deployment location. Is it available in deployment location ? Commented Dec 16, 2014 at 12:21
  • Please provide more details on how do you run your application in Tomcat. For example, if you export a WAR from Eclipse and deploy it manually in Tomcat, then you should check if the JARs are being exported correctly to the /lib folder within the WAR. Commented Dec 16, 2014 at 12:22
  • @sankar I had referred your suggested link and did the deployment path addition but that does not silve my problem Commented Dec 16, 2014 at 12:29
  • @JayaAnanthram I checked the deployment location n it is available there Commented Dec 16, 2014 at 12:30

4 Answers 4

36

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>
Sign up to request clarification or add additional context in comments.

Comments

33

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.

1 Comment

Where is the WAR when you mention "Right-click on WAR" in Eclipse?
2

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

Comments

-1

Add json jar to your classpath

or use java -classpath json.jar ClassName

refer this

1 Comment

I added the jar file in classpath by editing it in environment variables. That's the way to do it , right?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.