NoClassDefFoundError JsonAutoDetect while parsing JSON object
Solution 1
For those who come here in the future, the answer is:
If you've only copied jackson core
and jackson databind
, you need jackson annotations
to use the ObjectMapper
.
For example, make sure you have something like this:
[16:32:01]:/android-project/libs master]$ ls -lAF
total 2112
-rw-r--r--@ 1 jeffamaphone staff 33525 Jun 18 16:06 jackson-annotations-2.0.2.jar
-rw-r--r--@ 1 jeffamaphone staff 193693 Jun 7 16:42 jackson-core-2.0.2.jar
-rw-r--r--@ 1 jeffamaphone staff 847121 Jun 18 15:22 jackson-databind-2.0.2.jar
Solution 2
To expand on @jeffamaphone's excellent answer, for maven users
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.7.3</version>
</dependency>
Solution 3
Or the following gradle configuration:
compile 'com.fasterxml.jackson.core:jackson-core:2.2.2'
compile 'com.fasterxml.jackson.core:jackson-databind:2.2.2'
compile 'com.fasterxml.jackson.core:jackson-annotations:2.2.2'
Solution 4
If you use maven and have all libraries in place as in my case a simple mvn clean
and mvn install
fixed problem.
Related videos on Youtube
jhahn
Updated on June 03, 2020Comments
-
jhahn almost 4 years
I am developing a webapp using Amazon's cloud services and I need to make use of JSON objects. How my project is set up is, I have an HTML form where the user will fill in their information and submit. Once submitted the data will be placed into an online database and a confirmation email is sent to them. Before I can submit the data to the data base I need to place all of it into a JSON object. My servlet, done in Java, looks like this:
public class FormHandling extends HttpServlet { //doGet function public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // In this part of the code I take in the user data and build an HTML // page and return it to the user // Create String Mapping for User Data object Map<String,Object> userData = new HashMap<String,Object>(); // Map the names into one struct. In the code below we are creating // values for the nameStruct from the information the user has // submitted on the form. Map<String,String> nameStruct = new HashMap<String,String>(); nameStruct.put("fName", request.getParameter("fname")); nameStruct.put("lName", request.getParameter("lname")); nameStruct.put("mInit", request.getParameter("minit")); // Map the three email's together (providing there are 3). // This is the same logic as the names. Map<String,String> emailStruct = new HashMap<String,String>(); emailStruct.put("email", request.getParameter("email1")); emailStruct.put("email", request.getParameter("email2")); emailStruct.put("email", request.getParameter("email3")); // Put Name Hash Value Pair inside User Data Object userData.put("name", nameStruct); userData.put("emails", emailStruct); userData.put("age", 22); // Create the Json data from the userData createJson(userData); // Close writer out.close(); } public File createJson(Map<String,Object> userData) throws JsonGenerationException, JsonMappingException, IOException { File user = new File("user.json"); // Create mapper object ObjectMapper mapper = new ObjectMapper(); // Add the userData information to the json file mapper.writeValue(user, userData); return user; } }
When I submit the form using this code I get the following error message:
java.lang.NoClassDefFoundError: com/fasterxml/jackson/annotation/JsonAutoDetect com.fasterxml.jackson.databind.introspect.VisibilityChecker$Std.(VisibilityChecker.java:169) com.fasterxml.jackson.databind.ObjectMapper.(ObjectMapper.java:197) edu.tcnj.FormHandling.createJson(FormHandling.java:115) edu.tcnj.FormHandling.doGet(FormHandling.java:104) edu.tcnj.FormHandling.doPost(FormHandling.java:131) javax.servlet.http.HttpServlet.service(HttpServlet.java:641) javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
Now I know that error means that it isn't finding the Jackson databind library. I am developing in Eclipse and I know the build path messes up from time to time so I also placed the libraries in the WebContent\WEB-INF\lib folder. This has solved issues I've had before. However, this does not seem to solve my problem this time around. I have even opened the jar files to physically check that the library and class needed are there, and they are. I've tried searching all over and looking into different ways to include the library but nothing seems to work.
-
Knut Forkalsrud about 12 yearsCan you let us know which version of the Jackson library you are using?
-
Knut Forkalsrud about 12 yearsI need two JARs,
jackson-core-lgpl-1.5.6.jar
andjackson-mapper-lgpl-1.5.6.jar
-
jhahn about 12 yearsTurns out I was missing a third jar....ugh. So simple. For me I need to use the jackson databind, core, and annotation libraries. I'm using version 2.0. Once I found that third jar it was happy. Thank you though!
-
TheSteve0 over 10 yearsYou should mark the one below as the answeer
-
Admin almost 10 yearsCan you please comment the exact link to the annotations .jar file?
-
Atul over 7 yearsThis post helps in solving below issue also: NoClassDefFoundError com.fasterxml.jackson.annotation.ObjectIdResolver jackson jaxrs provider
-
-
ericsoco over 11 yearsand that can be downloaded from wiki.fasterxml.com/JacksonDownload. (save you a google.)
-
CorayThan about 10 yearsIs it even possible to write and read to and from Json with Jackson without the ObjectMapper?
-
jamil ahmed about 10 yearsAt the time, in my case, it was possible to read some JSON without it. I'm not sure what the state of things are now.
-
wired00 almost 10 yearsThank you, This worked for me after doing the Spring Acessing Twitter Data tutorial spring.io/guides/gs/accessing-twitter
-
Joel almost 10 yearsIsn't there a way to not use annotations? I tried MapperFeature.USE_ANNOTATIONS, but that seems to still need the jar.
-
Burke9077 over 8 yearsFor those visiting from the further future, here is the new download link: github.com/FasterXML/jackson-annotations/wiki#downloads