33

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

abhi
  • 1,760
  • 1
  • 24
  • 40
HitchHiker
  • 825
  • 2
  • 11
  • 31
  • Hope the below links helps http://stackoverflow.com/questions/14657585/java-lang-noclassdeffounderror-org-json-simple-parser-parseexception-with-eclip – sankar Dec 16 '14 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 ? – Jaya Ananthram Dec 16 '14 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. – AJPerez Dec 16 '14 at 12:22
  • @sankar I had referred your suggested link and did the deployment path addition but that does not silve my problem – HitchHiker Dec 16 '14 at 12:29
  • @JayaAnanthram I checked the deployment location n it is available there – HitchHiker Dec 16 '14 at 12:30
  • @AJPerez I put the .class file in classes folder of web-inf,edited the web.xml,restarted tomcat and gave the URL of html in browser. – HitchHiker Dec 16 '14 at 12:31
  • What is the exact jar name? and What is the import statement that you have given for the JSONObject? – Jaya Ananthram Dec 16 '14 at 12:33
  • Then you have to copy the JAR to WEB-INF/lib within your webapp in Tomcat. It doesn't matter that you already configured it in Eclipse, Tomcat doesn't know... – AJPerez Dec 16 '14 at 12:35
  • @JayaAnanthram The import statement is import org.json.JSONObject; and jar name is org.json-20120521.jar – HitchHiker Dec 16 '14 at 12:36
  • Are you sure? Did you verified whether org.json-20120521.jar is available in WEB-INF/lib? – Jaya Ananthram Dec 16 '14 at 12:41
  • @Jaya Ananthram I seemed to have mistaken the deployment location.I added it now. Thank you! – HitchHiker Dec 16 '14 at 12:44
  • @AJPerez I added the jar to WEB-INF/lib and it worked as expected. Thank you! – HitchHiker Dec 16 '14 at 12:44
  • But why can't I run it in Eclipse? – HitchHiker Dec 16 '14 at 12:47
  • Try by **clean and build** and then run your project. For Eclipse, adding a class path variable is enough to avoid class reference error in eclipse. Only if you add your library in your deployment assembly, library will be available on run time (jar will be moved to your lib) – Jaya Ananthram Dec 16 '14 at 12:52
  • @JayaAnanthram Still no luck! – HitchHiker Dec 16 '14 at 12:57
  • Did you redeployed (publish) your application? – Jaya Ananthram Dec 16 '14 at 12:58
  • @JayaAnanthram Well I did clean build and restarted server and run the servlet. Should any other steps be followed? – HitchHiker Dec 16 '14 at 13:00
  • @vaishnavee Did you added in deployment assembly of your war or some other project which is added as project reference to the WAR? – Jaya Ananthram Dec 16 '14 at 13:06
  • @JayaAnanthram I added it to my war – HitchHiker Dec 16 '14 at 13:10
  • Then it must work !!! There is no chance of flaws !!! Tried? – Jaya Ananthram Dec 16 '14 at 13:14
  • @JayaAnanthram Ya it should work, I guess I will have to run it w/o IDE afterall :( – HitchHiker Dec 16 '14 at 13:17
  • Nope It should work.. How you are adding the jar in classpath and deployment assembly. Just give me simple comments. – Jaya Ananthram Dec 16 '14 at 13:49
  • I added the jar file in classpath by editing variable in environment variables. gave path of jar file located on my desktop. – HitchHiker Dec 16 '14 at 13:56
  • and as I mentioned I added it in deployment assembly by giving the path of WEB-INF/lib/org.json-20120521.jar of eclipse project – HitchHiker Dec 16 '14 at 13:58
  • **I added the jar file in classpath by editing variable in environment variables.** Do you mean about MyComputer(right click)->Properties->Advanced Settings->Environment variable? – Jaya Ananthram Dec 16 '14 at 14:07
  • Refer my answer below. – Jaya Ananthram Dec 16 '14 at 14:35

4 Answers4

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>
Raghu K Nair
  • 3,854
  • 1
  • 28
  • 45
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.

spaaarky21
  • 6,524
  • 7
  • 52
  • 65
Jaya Ananthram
  • 3,433
  • 1
  • 22
  • 37
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

gprathour
  • 14,813
  • 5
  • 66
  • 90
-1

Add json jar to your classpath

or use java -classpath json.jar ClassName

refer this

Community
  • 1
  • 1
Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116