Getting java.lang.NoClassDefFoundError when using the Google Gson Library

I use the Gson library to convert objects to Json and vice versa. I copied the jars this library to a new lib folder and added it to the build path for the project in my Eclipse IDE .

I have one main class in which I convert the object to Json using toJson() , shown below, and send it to the servlet using Apache HttpClient HttpPost() .

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

But in servlet I cannot convert Json to Object using fromJson() when I execute the following code.

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

This throws the following exception:

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

Any idea why this might happen? Should I copy jars to the WebContent/WEB-INF/lib folder instead of a new folder named lib ?

+4
source share
1 answer

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

I get "java.lang.ClassNotFoundException: com.google.gson.Gson" although it is defined in my classpath

I just had to put the external jars in my WebContent/WEB-INF/lib folder, and Eclipse will take care of the build path .

+3
source

Source: https://habr.com/ru/post/1438540/


All Articles