Original link: https://hsiaofeng.com/archives/222.html
There is a Maven project written in IDEA. After adding dependencies through pom.xml
, it still runs and prompts java.lang.ClassNotFoundException
. After asking ChatGPT to get a solution, it is recorded.
solution
Click the Maven
tab on the right, double-click to open the project name, and double-click to select install
under Lifecycle
.
You can also open the Terminal
tab below and run the command
mvn install
background
Add Maven dependencies in File
– Project Structure...
window in IDEA, run the Java Web project, and report an error:
例外情况jakarta.servlet.ServletException: Servlet执行抛出一个异常org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)根本原因。 java.lang.NoClassDefFoundError: com/google/gson/JsonObject com.example.webstudy.ReturnJSON.doGet(ReturnJSON.java:24) jakarta.servlet.http.HttpServlet.service(HttpServlet.java:668) jakarta.servlet.http.HttpServlet.service(HttpServlet.java:777) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)根本原因。 java.lang.ClassNotFoundException: com.google.gson.JsonObject org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1449) org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1257) com.example.webstudy.ReturnJSON.doGet(ReturnJSON.java:24) jakarta.servlet.http.HttpServlet.service(HttpServlet.java:668) jakarta.servlet.http.HttpServlet.service(HttpServlet.java:777) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Ask ChatGPT and get the following reply:
If you use Maven, you need to add the Gson dependency in the pom.xml file first.
<dependencies> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.9</version> </dependency> </dependencies>
Then, you need to rebuild your Maven project and let Maven download and install the dependencies.
In IDEA, you can find Lifecycle in the Maven tab on the right, find install in it, right-click to run, or run mvn install directly on the command line to let Maven install dependencies. After doing this, IDEA should be able to find the com.google.gson.JsonObject class.
This article is transferred from: https://hsiaofeng.com/archives/222.html
This site is only for collection, and the copyright belongs to the original author.