英文:
Gson Class Imported by Maven is Not Found When Deployed on JSP and Servlet Java Web
问题
我通过Maven导入了Gson,其代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>HerbTea</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.4.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
然而,当我在JSP Servlet程序上运行我的程序时,我收到以下错误:
java.lang.NoClassDefFoundError: com/google/gson/Gson
我不知道为什么会出现这个错误。您可以在这里查看错误报告:链接
HTTP状态500 – 内部服务器错误
类型异常报告
消息处理[/outcome.jsp]中的异常在第[21]行发生了异常
描述服务器遇到了一个意外情况,阻止了它完成请求。
异常
org.apache.jasper.JasperException:在第[21]行处处理[/outcome.jsp]中的异常
18: <% WindowDisplay windowDisplay = (WindowDisplay) request.getSession().getAttribute("windowDisplay");
19: System.out.println(session.getAttribute(reqSymptomParamKey));
20: windowDisplay.getRecipeDisplay();
21: List<Recipe> a = windowDisplay.getRecipeDisplay().retrieveCorrespondingRecipe((List<String>) session.getAttribute(reqSymptomParamKey));
22: for (Recipe i : windowDisplay.getRecipeDisplay().retrieveCorrespondingRecipe((List<String>) session.getAttribute(reqSymptomParamKey))) {%>
23: <p1>name:<%=i.getName()%> </p1>
24: <p1>Corresponding Symptom: <%=i.getSymptomNameList()%></p1>
堆栈跟踪:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:617)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:499)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
com.web.servlet.ServerServlet.doPost(ServerServlet.java:60)
javax.servlet.http.HttpServlet.service(HttpServlet.java:652)
javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
根本原因
javax.servlet.ServletException:java.lang.NoClassDefFoundError:com/google/gson/Gson
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:909)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:838)
org.apache.jsp.outcome_jsp._jspService(outcome_jsp.java:178)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:71)
javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:476)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
com.web.servlet.ServerServlet.doPost(ServerServlet.java:60)
javax.servlet.http.HttpServlet.service(HttpServlet.java:652)
javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
根本原因
java.lang.NoClassDefFoundError:com/google/gson/Gson
根据调试器的信息,问题发生在以下行,因为找不到Gson包中的任何类:
GsonBuilder gsonBuilder = new GsonBuilder();
有人可以帮助我解决这个java.lang.NoClassDefFoundError:com/google/gson/Gson
问题吗?
英文:
I imported Gson through maven whose code is
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>HerbTea</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.4.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
However, when I run my program on JSP sevlet program, I receive
java.lang.NoClassDefFoundError: com/google/gson/Gson
and I don't know why.
You can see the error report here
HTTP Status 500 – Internal Server Error
________________________________________
Type Exception Report
Message An exception occurred processing [/outcome.jsp] at line [21]
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
org.apache.jasper.JasperException: An exception occurred processing [/outcome.jsp] at line [21]
18: <% WindowDisplay windowDisplay = (WindowDisplay) request.getSession().getAttribute("windowDisplay");
19: System.out.println(session.getAttribute(reqSymptomParamKey));
20: windowDisplay.getRecipeDisplay();
21: List<Recipe> a = windowDisplay.getRecipeDisplay().retrieveCorrespondingRecipe((List<String>) session.getAttribute(reqSymptomParamKey));
22: for (Recipe i : windowDisplay.getRecipeDisplay().retrieveCorrespondingRecipe((List<String>) session.getAttribute(reqSymptomParamKey))) {%>
23: <p1>name:<%=i.getName()%> </p1>
24: <p1>Corresponding Symptom: <%=i.getSymptomNameList()%></p1>
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:617)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:499)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
com.web.servlet.ServerServlet.doPost(ServerServlet.java:60)
javax.servlet.http.HttpServlet.service(HttpServlet.java:652)
javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Root Cause
javax.servlet.ServletException: java.lang.NoClassDefFoundError: com/google/gson/Gson
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:909)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:838)
org.apache.jsp.outcome_jsp._jspService(outcome_jsp.java:178)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:71)
javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:476)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
com.web.servlet.ServerServlet.doPost(ServerServlet.java:60)
javax.servlet.http.HttpServlet.service(HttpServlet.java:652)
javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Root Cause
java.lang.NoClassDefFoundError: com/google/gson/Gson
The line where the problem occurs according to debugger is this one because any class in Gson package is not found:
GsonBuilder gsonBuilder = new GsonBuilder();
Can somebody help me to get over this java.lang.NoClassDefFoundError: com/google/gson/Gson
problem?
答案1
得分: 0
我最终自己找到了问题的答案。感谢那些帮助我编辑问题的人。
问题是我没有将Maven库添加到构件中。
解决方法是进入Intellij中的项目结构->构件->输出布局,然后在“可用元素”面板下点击相应的Maven导入的jar,将Gson库的jar添加到构件中。
英文:
I found out the question answer myself eventually. Thanks for those helping me in editing the question.
The problem is that I didn't add the Maven libaray into the artifact.
The solution will be going into Project Structure->Artifacts->Output layout in Intellij and click on the corresponding Maven imported jar under the "Available Elements" panel to add that Gson libaray jar into the artifact.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论