java.lang.ClassNotFoundException for servlet in tomcat with eclipse

CodeMed picture CodeMed · Aug 1, 2013 · Viewed 91.8k times · Source

I am starting to develop a java web application in eclipse using servlets and am testing it with tomcat server on my localhost. I have deployed the application in tomcat, but when I try to load the target url in my browser, I get the following stack trace:

Jul 31, 2013 2:58:31 PM org.apache.catalina.core.ApplicationContext log
INFO: Marking servlet ImageServlet as unavailable
Jul 31, 2013 2:58:31 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Allocate exception for servlet ImageServlet
java.lang.ClassNotFoundException: test.ImageServlet
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
    at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:527)
    at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:509)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:137)
        at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1144)
    at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:865)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:136)
    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.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1852)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)  

The ImageServlet class is quite clearly located in the myproject/src/test folder in my eclipse workspace, where myproject is the name of the eclipse project, and test is the package.

web.xml is located in myproject/web/WEB-INF/web.xml and myproject.xml is located at myproject/myproject.xml

The contents of web.xml are:

<?xml version="1.0"?>
<web-app 
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">

    <servlet>
        <servlet-name>ImageServlet</servlet-name>
        <servlet-class>test.ImageServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>ImageServlet</servlet-name>
        <url-pattern>/image</url-pattern>
    </servlet-mapping>
</web-app>

And the contents of myproject.xml are:

Can anyone show me how to fix my code so that it does not throw the ClassNotFoundException?

Answer

srinivas picture srinivas · Jun 30, 2014

Sometimes on creating of filters or servlets, the class file is not generated in build folder in eclipse.Clean the application and build it once, a .class file is generated in that above said path. This removes class not found error in some cases.