0

I have been using JSONObject from org.json in my Servlet class. However, when I try to run the app I get the following error:

Caused by: java.lang.NoClassDefFoundError: Lorg/json/JSONObject;
at java.lang.Class.getDeclaredFields0(Native Method)
at java.lang.Class.privateGetDeclaredFields(Class.java:2583)
at java.lang.Class.getDeclaredFields(Class.java:1916)
at org.apache.catalina.util.Introspection.getDeclaredFields(Introspection.java:110)
at org.apache.catalina.startup.WebAnnotationSet.loadFieldsAnnotation(WebAnnotationSet.java:262)
at org.apache.catalina.startup.WebAnnotationSet.loadApplicationServletAnnotations(WebAnnotationSet.java:136)
at org.apache.catalina.startup.WebAnnotationSet.loadApplicationAnnotations(WebAnnotationSet.java:66)
at org.apache.catalina.startup.ContextConfig.applicationAnnotationsConfig(ContextConfig.java:328)
at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:778)
at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:299)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:94)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5087)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 42 more
Caused by: java.lang.ClassNotFoundException: org.json.JSONObject
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1285)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1119)
... 55 more

I have no idea why this is happening only in the Servlet class. I verified running a standalone program using JSONObject in the same module and it ran fine. This issue is happening only when I use the JSONObject in the doPost method of my Servlet. I wasted lot of time on this and wanted some help badly. I'm running the app in IntelliJ.

Sri
  • 573
  • 2
  • 6
  • 20
  • What version of JDK are you using? – donlys Nov 18 '16 at 18:54
  • Check out this answer: http://stackoverflow.com/questions/18346609/how-can-i-import-javax-json-in-eclipse – donlys Nov 18 '16 at 18:55
  • I am using JDK 1.8 – Sri Nov 18 '16 at 18:56
  • Well its running fine in a standalone program in the same module. The issue happens only in the Servlet code. I have added the maven dependency, made sure the libraries exist. – Sri Nov 18 '16 at 18:58
  • Catalina might be running in jdk6. Can you double check catalina config file? – donlys Nov 18 '16 at 19:00
  • Catalina uses 1.8 only. I configured the Tomcat in IntelliJ and it was fine. – Sri Nov 18 '16 at 19:11
  • Its resolved then - right? – donlys Nov 18 '16 at 19:55
  • No. I mean Tomcat was running fine. The issue is still not resolved. – Sri Nov 18 '16 at 19:58
  • Can you try adding java-json.jar to your app as suggested in: http://stackoverflow.com/questions/16362302/using-org-json-in-servlet-causes-java-lang-classnotfoundexception-org-json-json – donlys Nov 18 '16 at 20:17
  • Well, I have tried using GSON. But I get the same issue with it too. ClassNotFOund. So it looks like no thrid party library is available at runtime. How do I fix it? – Sri Nov 19 '16 at 00:03
  • Did you try adding java-json.jar to the deployment assembly WEB-INF/lib? – donlys Nov 19 '16 at 14:12
  • Simplest step: create a new project with similar configurations, add same jar and check if it works. This way, you can see if it's a project/IDE issue or not... – diogo Nov 20 '16 at 17:39

0 Answers0