6

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?

Rajath
  • 2,178
  • 6
  • 32
  • 38

1 Answers1

5

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.

Community
  • 1
  • 1
Rajath
  • 2,178
  • 6
  • 32
  • 38
  • Its good that you found solution but Just FYI `java.lang.NoClassDefFoundError` and `java.lang.ClassNotFoundException` are different things. – Hardik Mishra Oct 09 '12 at 05:44
  • I got both the type of exceptions when I executed different Dynamic Web Projects with the same code. But thanks! Will take into note. – Rajath Oct 09 '12 at 05:52