Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.ClassNotFoundException for servlet in tomcat with eclipse [duplicate]

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?

like image 287
CodeMed Avatar asked Jul 31 '13 22:07

CodeMed


3 Answers

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.

like image 193
srinivas Avatar answered Sep 19 '22 19:09

srinivas


  1. Right click project and go to "Properties"
  2. Go to "Deployment Assembly"
  3. Ensure you have two mappings as given in the following image. Web deployment assembly in eclipse . If not then add the missing mappings.
  4. Click apply.
  5. Clean,build and run your project.
like image 30
Mrinal N Avatar answered Sep 18 '22 19:09

Mrinal N


Recently encountered this problem with one of my projects(I say this because the other worked perfectly fine despite using the same JARs and all). Tried cleaning the project multiple times, deleted the .metadata folder multiple times. Nothing worked. The classes simply weren't compiled.

What worked was removing an unbound jre6 library in the Build Path of the project.

like image 28
anuvab1911 Avatar answered Sep 21 '22 19:09

anuvab1911