0

I running a Java application on an Amazon EC2 instance which is a linux server. I included a json jar in the build path and I checked the class path and it's there. When I import the class

import org.json.*;

I dont get any compilation errors, but when I execute my app I get the ClassNotFoundException.

Exception in thread "Thread-1" java.lang.NoClassDefFoundError: org/json/JSONException
    at Group.run(Group.java:62)
    at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.ClassNotFoundException: org.json.JSONException
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)

Does anybody know how to fix it?

itaied
  • 6,827
  • 13
  • 51
  • 86
  • 1
    First it's considered bad coding practice to use imports like import org.json.*; Change it to import org.json.JSONException and see if your code still compiles. If you are using eclipse use organize imports to organize them for you. Also please give the version and name of the jar file that you are including. – vkg May 17 '14 at 17:25
  • Clean & build your project, chances are a compilation error suddenly shows up now which prevented your class file from being generated earlier. – Jeroen Vannevel May 17 '14 at 17:35
  • I changed it to JSONException, still doesnt work. I have downloaded the jar from here: [link](http://www.java2s.com/Code/Jar/j/Downloadjavajsonjar.htm) I have also cleaned and build the project, no compilation error. – itaied May 17 '14 at 20:10

1 Answers1

0

use java decompiler tools and check whether class json exception exists in your jar or else include the necessary jar.

geddamsatish
  • 174
  • 10
  • It does exists in the jar. – itaied May 17 '14 at 20:07
  • then it can be the case where two jars have the same class name.check whether you have mutliple versions of json or some other jar which has jsonexception class.Second can you simply import JSONExcepiton without doing .* one – geddamsatish May 17 '14 at 20:08
  • Only one jar... I also changed it to import only JSONException, still doesnt work. I tried to use another jar called simpleJson, and I get the same exception just with another class name: `at Group.run(Group.java:62) at java.lang.Thread.run(Thread.java:744) Caused by: java.lang.ClassNotFoundException: org.json.simple.parser.ParseException` – itaied May 17 '14 at 20:50
  • http://stackoverflow.com/questions/11346180/servlet-java-lang-classnotfoundexception-org-json-simple-parser-parseexception check this and json-simple jar – geddamsatish May 17 '14 at 20:52