Question

I am using Gson library to convert objects to Json and vice versa. I have copied the jars of that library into a new folder lib and added it to the build path for the project in my Eclipse IDE.

I have one main class where I convert an object into Json using toJson() shown below and send it to a servlet using Apache HttpClient's HttpPost().

Gson gson= new Gson();
String json = gson.toJson(names);

But in the servlet I am not able to convert the Json into Object using fromJson() when I execute the following code.

Gson gson = new Gson();
Names names = gson.fromJson(s, Names.class);

It is throwing the following exception:

java.lang.NoClassDefFoundError: com/google/gson/Gson

Any idea why that could be happening? Should I have copied the jars into the WebContent/WEB-INF/lib folder instead of a new folder called lib?

Was it helpful?

Solution

Okay, I figured out the answer using this post. Please read the comments in the first answer.

I am getting "java.lang.ClassNotFoundException: com.google.gson.Gson" error even though it is defined in my classpath

I just had to place the external jars into my WebContent/WEB-INF/lib folder and Eclipse would take care of the build path itself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top