others-how to solve `java.lang.NoClassDefFoundError: com/alibaba/fastjson/JSONObject` error when dealing with json in java?
1. Purpose
In this post, I will demonstrate how to solve the following error when using java to deal with json:
Servlet[appServlet]的Servlet.service()Handler processing failed; nested exception is java.lang.NoClassDefFoundError: com/alibaba/fastjson/JSONObject
java.lang.NoClassDefFoundError: com/alibaba/fastjson/JSONObject
at com.app.controllers.AppController.getTimerOrderInfo(AppController.java:80)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:747)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:676)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at com.opensymphony.sitemesh.webapp.SiteMeshFilter.obtainContent(SiteMeshFilter.java:129)
at com.opensymphony.sitemesh.webapp.SiteMeshFilter.doFilter(SiteMeshFilter.java:77)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:492)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:165)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:1025)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:451)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1201)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:654)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:317)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
2. Solution
2.1 What is fastjson?
Fastjson is a high-performance JSON processing library for Java, which is widely used for parsing JSON strings into Java objects and converting Java objects into JSON strings. It is developed by Alibaba and is known for its speed and efficiency, especially in scenarios where large volumes of JSON data need to be processed quickly.
Here are some key features of Fastjson:
-
Performance: It is one of the fastest JSON libraries for Java, often outperforming other popular libraries in terms of speed.
-
Simplicity: It provides a straightforward API for parsing and generating JSON data.
-
Feature-rich: It supports a wide range of features, including but not limited to:
- Flexible parsing with lenient modes.
- Support for various data formats, including Java Bean, Collections, Maps, and primitive types.
- Custom serialization and deserialization through interfaces.
- Support for annotations to control serialization and deserialization behavior.
-
Compatibility: It is compatible with various Java versions and can be easily integrated into different types of Java projects.
-
Community and Support: Given its popularity, there is a good amount of community support and documentation available.
To use Fastjson in a Java project, you typically include it as a dependency in your build configuration file (like pom.xml for Maven or build.gradle for Gradle), and then you can start parsing and generating JSON data.
Here’s an example of how you might include Fastjson in your Maven pom.xml:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.78</version> <!-- Use the latest version available -->
</dependency>
And here’s a simple example of using Fastjson to parse a JSON string into a Java object and serialize a Java object into a JSON string:
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
public class FastjsonExample {
public static void main(String[] args) {
// Parsing JSON string to Java object
String jsonString = "{\"name\":\"Kimi\", \"age\":30}";
User user = JSON.parseObject(jsonString, User.class);
// Serializing Java object to JSON string
String userJson = JSON.toJSONString(user);
System.out.println(userJson);
}
}
class User {
private String name;
private int age;
// Constructors, getters, and setters would be here
}
2.2 Debug
classpath:
[root@iZ23x64v3hjZ tomcat-appwebssl]# find . -name "*.jar"
./lib/websocket-api.jar
./lib/tomcat-i18n-ja.jar
./lib/jsp-api.jar
./lib/catalina.jar
./lib/servlet-api.jar
./lib/tomcat-jdbc.jar
./lib/jasper.jar
./lib/tomcat7-websocket.jar
./lib/tomcat-i18n-ru.jar
./lib/ecj-4.4.2.jar
./lib/catalina-tribes.jar
./lib/jasper-el.jar
./lib/tomcat-util.jar
./lib/tomcat-api.jar
./lib/annotations-api.jar
./lib/tomcat-i18n-zh-CN.jar
./lib/catalina-ant.jar
./lib/catalina-ha.jar
./lib/tomcat-i18n-es.jar
./lib/el-api.jar
./lib/tomcat-i18n-ko.jar
./lib/tomcat-i18n-fr.jar
./lib/tomcat-coyote.jar
./lib/tomcat-i18n-de.jar
./lib/tomcat-dbcp.jar
./app/lib/javax.inject-1.jar
./app/lib/lucene-highlighter-5.3.1.jar
./app/lib/jsr311-api-1.1.1.jar
./app/lib/commons-pool2-2.3.jar
./app/lib/guava-18.0.jar
./bin/bootstrap.jar
./bin/tomcat-juli.jar
./bin/commons-daemon.jar
2.3 The solution to this problem
According to the debug log, we can see that the fastjson jar is missing in our classpath, so copy it to the classpath:
./lib/fastjson-1.2.78.jar
3. Summary
In this post, I demonstrated how to solve the java.lang.NoClassDefFoundError: com/alibaba/fastjson/JSONObject
. That’s it, thanks for your reading.