GSON with a servlet

4,344 views
Skip to first unread message

arunjitsingh

unread,
Jun 22, 2010, 3:26:49 AM6/22/10
to google-gson
I created some test classes to try out GSON before I use it, and it
rocks!
Simple Java classes import the library fine, and it executes well..
but, when I use it with a servlet (the main reason for using this
library), it immediately crashes, despite the code and import being
the same! No compilation errors, just a 500 error when I GET the
servlet.

HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented
it from fulfilling this request.

exception

javax.servlet.ServletException: Servlet execution threw an exception
root cause

java.lang.NoClassDefFoundError: com/google/gson/Gson
ajs.web.TestServlet.doGet(TestServlet.java:39)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause

java.lang.ClassNotFoundException: com.google.gson.Gson

org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:
1516)

org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:
1361)
ajs.web.TestServlet.doGet(TestServlet.java:39)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
note The full stack trace of the root cause is available in the Apache
Tomcat/6.0.26 logs.

inde...@gmail.com

unread,
Jun 23, 2010, 4:42:11 PM6/23/10
to google-gson
Seems like a classpath issue in your app. Did you put gson jar in the
application library path or somewhere else?

Inder

arunjitsingh

unread,
Jun 24, 2010, 10:00:54 AM6/24/10
to google-gson
I am using Eclipse..
I copied the 3 jars (the classes, source and javadocs) into my active
project and set the build path in the project settings.
The real problem is that if I create a java class that is not a
servlet, ie., a class with a main() function (in the same package),
the Gson classes work fine.

So, something like ..

ajs.web.TestServlet#doGet(..) which uses Gson#toJSON(new
String("JSON"))
does not work and shows the error

however
ajs.web.TestLocal#main(..) using Gson#toJSON(new String("JSON"))
prints to System.out fine..

I'm flabbergasted and getting behind on my project but nothing I try
works!
I'm not so good at using Servlets (more of a front-end developer), so
this is a bit frustrating!

I really need JSON as my front-end is entirely javascript, and this
looks like the best and easiest library to use! Maybe tonight I'll
checkout the source and put it directly in my project. :(

Any help is appreciated!

--Arunjit

arunjitsingh

unread,
Jun 25, 2010, 12:12:20 PM6/25/10
to google-gson
OK,
So I checked out the source and copied the "com" folder tree to the
source of my project..
And?

And...
It works!
Somehow, that was expected.. I did some digging around and it is
possible that the servlet was not able to load the jar classes in its
ClassLoader context. (I don't remember all the pages I visited while
speed-surfing, but here's a white-paper that helped:
http://media.techtarget.com/tss/static/articles/content/dm_classForname/DynLoad.pdf).
The dynamic loading (using the Thread's context ClassLoader) did not
work either (but at least I was able to gracefully crash the app
instead of throwing out a 500 error).

So, the svn source worked.

I would still like to know if it is possible to do without the source
(I don't think I'll be modifying the source.. it's a bit above my
skill-set)

Thanks for any help

--Arunjit

cgarethc

unread,
Jun 25, 2010, 9:59:38 PM6/25/10
to google-gson
The jars should be living in the WEB-INF/lib directory of your
deployed application. It depends on how you set up your project in
Eclipse as to which directory gets copied there when you build your
app.

On Jun 26, 4:12 am, arunjitsingh <arunjitsingh1...@gmail.com> wrote:
> OK,
> So I checked out the source and copied the "com" folder tree to the
> source of my project..
> And?
>
> And...
> It works!
> Somehow, that was expected.. I did some digging around and it is
> possible that the servlet was not able to load the jar classes in its
> ClassLoader context. (I don't remember all the pages I visited while
> speed-surfing, but here's a white-paper that helped:http://media.techtarget.com/tss/static/articles/content/dm_classForna...).
Reply all
Reply to author
Forward
0 new messages