0

I'm new to servlets, I got java.lang.ClassNotFoundException whenever I try to include JSON library, I search the web and stackoverflow the only suggestion I found is to install dependencies I've Tried org.json , net.sf.json each with its dependencies nothing works and gives the same exception.

Any idea?

4
  • 1
    Which JSON library in particular are you trying to use? Where did you include it? Commented Nov 3, 2013 at 13:59
  • I tried org.json then net.sf.json copied the libraries to WebContent\lib and included them in build path Commented Nov 3, 2013 at 14:01
  • Yes but which class is it that gives you a ClassNotFoundException? Is that class in those jars? Commented Nov 3, 2013 at 14:03
  • the JSONObject class Commented Nov 3, 2013 at 14:04

2 Answers 2

6

JSONObject is contained in java-json.jar. Make sure that have you have java-json.jar in your classpath. There are multiple places to put jars in tomcat depending which class loader you want the class to load.

If this jar is only required by your application then simply put it under your application directory in

WEB-INF/lib

It is worth reading how classes are loaded in Tomcat to place your jars optimally: http://tomcat.apache.org/tomcat-7.0-doc/class-loader-howto.html

Sign up to request clarification or add additional context in comments.

2 Comments

thanks it works, but how and why do i need to change class loader?
@Warlock You don't have to change the class loader. But you should be aware how classes are loaded in Tomcat so that you can optimally places the jars. Read this: tomcat.apache.org/tomcat-7.0-doc/class-loader-howto.html
4

Try to put the libs for your web app in the directory

WebContent/WEB-INF/lib

1 Comment

+1: And DON'T put it in the build path. Everything under WEB-INF/lib is automatically added to the build path by Eclipse.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.