Gson类通过Maven导入,在部署到JSP和Servlet Java Web时找不到。

huangapple go评论75阅读模式
英文:

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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
         xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
         xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

    &lt;groupId&gt;org.example&lt;/groupId&gt;
    &lt;artifactId&gt;HerbTea&lt;/artifactId&gt;
    &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
    &lt;!-- https://mvnrepository.com/artifact/com.google.code.gson/gson --&gt;
    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;com.google.code.gson&lt;/groupId&gt;
            &lt;artifactId&gt;gson&lt;/artifactId&gt;
            &lt;version&gt;2.8.5&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api --&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
            &lt;artifactId&gt;junit-jupiter-api&lt;/artifactId&gt;
            &lt;version&gt;5.4.0&lt;/version&gt;
            &lt;scope&gt;test&lt;/scope&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
            &lt;artifactId&gt;junit-jupiter-api&lt;/artifactId&gt;
            &lt;version&gt;5.4.0&lt;/version&gt;
            &lt;scope&gt;compile&lt;/scope&gt;
        &lt;/dependency&gt;


    &lt;/dependencies&gt;



&lt;/project&gt;

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:     &lt;% WindowDisplay windowDisplay = (WindowDisplay) request.getSession().getAttribute(&quot;windowDisplay&quot;);
19:         System.out.println(session.getAttribute(reqSymptomParamKey));
20:         windowDisplay.getRecipeDisplay();
21:         List&lt;Recipe&gt; a = windowDisplay.getRecipeDisplay().retrieveCorrespondingRecipe((List&lt;String&gt;) session.getAttribute(reqSymptomParamKey));
22:         for (Recipe i : windowDisplay.getRecipeDisplay().retrieveCorrespondingRecipe((List&lt;String&gt;) session.getAttribute(reqSymptomParamKey))) {%&gt;
23:     &lt;p1&gt;name:&lt;%=i.getName()%&gt; &lt;/p1&gt;
24:     &lt;p1&gt;Corresponding Symptom: &lt;%=i.getSymptomNameList()%&gt;&lt;/p1&gt;


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.

huangapple
  • 本文由 发表于 2020年7月30日 22:38:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63175516.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定