java.lang.ClassNotFoundException: com.google.gson.Gson

三世轮回 提交于 2019-12-11 19:39:34

问题


When I send a response from servlet to ajax, I get ClassNotFoundException about Gson class.

Here is the stacktrace:

java.lang.ClassNotFoundException: com.google.gson.Gson
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
    at com.jotform.demo.write(demo.java:64)
    at com.jotform.demo.doPost(demo.java:53)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

This is my code:

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        Map<String,Object> map= new HashMap<String,Object>();
        boolean isValid = false;
        String us=request.getParameter("q1_abc");
        System.out.println(us);

        if(us != null  &&  us.trim().length() != 0 ){
            isValid=true;
            map.put("us",us);
        }
        map.put("isValid",isValid);
        write(response,map);
    }

    private void write(HttpServletResponse response, Map<String,Object> map) throws IOException {
         response.setContentType("application/json");       
         response.setCharacterEncoding("UTF-8");
         response.getWriter().write(new Gson().toJson(map));
    }

I get an error on line that executes response.getWriter().write(new Gson().toJson(map));.

I added Gson jar, every kind (sources,javadoc), but I don't understand. I can get a form object, but I can't get a response.

To give more details, in Firefox debugger the error is: [HTTP/1.1 500 Internal Server Error 15 ms]

来源:https://stackoverflow.com/questions/18620336/java-lang-classnotfoundexception-com-google-gson-gson

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!