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

7

14

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?

Transmit answered 31/7, 2013 at 22:41 Comment(1)
In my case, I found out that the build path of the project is not referring to a valid servlet-api.jar file. So I configured the build path by pointing to valid jar file and removing the older reference.Bicephalous
P
25

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.

Pyramid answered 30/6, 2014 at 17:16 Comment(3)
+1 and thank you for taking the time to answer this old question. I hope that your answer helps other people.Transmit
Don't forget to enable automatic builds Project > Build AutomaticallyLuke
"Enable automatic builds Project > Build Automatically" Helped my and cleaning project too. Thanks aliopiLotion
C
7
  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.
Crenelation answered 27/7, 2016 at 11:8 Comment(0)
C
3

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.

Coextend answered 24/12, 2015 at 6:59 Comment(0)
R
2

it is a maven project? in maven projects src/test is the location for unit tests and is excluded from build and war, try change the package name and try again.

Renewal answered 31/7, 2013 at 22:44 Comment(1)
Thanks, but it is not a maven project. See my comment to duffymo . +1 for trying to help. Any other suggestions?Transmit
S
2

Looks like youre building a Maven-based project, and you're putting your class on the wrong location

You should use src/main/java to put your classes, and src/test/java to put your tests on it (ie, JUnit tests, if you have any).

Sigismundo answered 31/7, 2013 at 22:48 Comment(1)
@Christian Meneses Thanks, but this is not a maven project. +1 for trying to help. Any other ideas?Transmit
M
2

If your project is a Maven project, you could give a try to cleaning it. Click Project > Clean and select the desired project to clean.

Sometimes, if you use Maven with either console and Eclipse, the target gets "crazy" and the only way to go on is cleaning the project.

Another times, you must add the corresponding server runtime within project facets (e.g. Tomcat 9.0).

Mizzenmast answered 5/8, 2016 at 7:53 Comment(0)
O
1

If there's no class test.ImageServlet in your WEB-INF/classes deployment, then Tomcat won't be able to find it.

My guess is the you didn't compile, package, and deploy the WAR file properly to Tomcat.

Oscar answered 31/7, 2013 at 22:44 Comment(2)
@dyffymo Thanks, but I am using a context path and an XML configuration file to deploy from within the Tomcat manager application. This approach has worked with other applications, without using WAR files. Can you suggest something in line with the approach I am taking? +1 for trying to help.Transmit
No, I'd recommend creating a proper WAR file. I wouldn't be fooled by the fact that it "worked" once before. A WAR is the right thing. Reliance on Eclipse is not.Oscar

© 2022 - 2024 — McMap. All rights reserved.