Search code examples
javaservletsgsonclassnotfoundexception

java.lang.ClassNotFoundException: com.google.gson.Gson despite having the JAR in the Apache server


Read many posts regarding the same error on Stackoverflow. But I am still getting the following error (Stacktrace):

Jul 31, 2014 3:08:54 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet TEST_Authenticate threw exception
java.lang.ClassNotFoundException: com.google.gson.Gson
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
    at TEST_Authenticate.doGet(TEST_Authenticate.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:744)

Line #70 is as follows in my code:

out.println(new Gson().toJson(TESTAuthenticate("[email protected]")));

I am using Netbeans and here is the structure of my project.

enter image description here

I have included the JARs in my server lib folder as shown in the image above.

Someone answered in this post "did you added the jar to the /WEB-INF/lib folder of your webapp? ", however, I don't see lib folder inside the WEB-INF as shown in the image above. I am wondering what am I doing wrong?

Updated New Screenshot showing gson-2.2.4.jar

enter image description here


Solution

  • I never used NetBeans but I think you have to make a difference between the internal classpath for the Tomcat engine itself and the classpath of your application. Tomcat separates this so the web applications are not required to use the same versions of libraries Tomcat uses. Isn't there another way to define a dependency of your project with the gson library ? F.e. using maven dependency and generate the project file.

    Second thing I notice: what is the content of the gson-2.2.4-javadoc.jar ? does it contains classes or just javadoc html files.

    Normally when building the war file (using maven) these dependencies will be added to the WEB-INF/lib folder in the generated .war file.