问题
This question already has an answer here:
- Servlet returns “HTTP Status 404 The requested resource (/servlet) is not available” 9 answers
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?
回答1:
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.
回答2:
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.
回答3:
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.
回答4:
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).
回答5:
- Right click project and go to "Properties"
- Go to "Deployment Assembly"
- Ensure you have two mappings as given in the following image. Web deployment assembly in eclipse . If not then add the missing mappings.
- Click apply.
- Clean,build and run your project.
回答6:
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.
回答7:
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).
来源:https://stackoverflow.com/questions/17982240/java-lang-classnotfoundexception-for-servlet-in-tomcat-with-eclipse