Cannot start servlet from tomcat server even if classes are in package

I try to run my servlet, I searched for 2 days and try all possible solutions and no luck. The servlet class is located in the appropriate folder (that is, under the package name). I also added the jar files needed in my servlet to the lib folder. The web.xml file maps the URL and defines the servlet. Therefore, I did everything that was in the documentation, and people spoke here and still get this error:

type Exception report message description The server encountered an internal error () that prevented it from fulfilling this request. exception javax.servlet.ServletException: Error instantiating servlet class assign1a.RPCServlet org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:108) org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:558) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:379) org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:282) org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:357) org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1687) java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) java.lang.Thread.run(Thread.java:619) root cause java.lang.NoClassDefFoundError: assign1a/RPCServlet (wrong name: server/RPCServlet) java.lang.ClassLoader.defineClass1(Native Method) java.lang.ClassLoader.defineClassCond(ClassLoader.java:632) java.lang.ClassLoader.defineClass(ClassLoader.java:616) java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141) org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2820) org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1143) org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1638) org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1516) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:108) org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:558) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:379) org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:282) org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:357) org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1687) java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) java.lang.Thread.run(Thread.java:619) note The full stack trace of the root cause is available in the Apache Tomcat/7.0.5 logs. 

Also here is my servlet code:

 package assign1a; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import lib.jsonrpc.RPCService; public class RPCServlet extends HttpServlet { /** * */ private static final long serialVersionUID = -5274024331393844879L; private static final Logger log = Logger.getLogger(RPCServlet.class.getName()); protected RPCService service = new ServiceImpl(); public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); response.getWriter().write("rpc service " + service.getServiceName() + " is running..."); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { try { service.dispatch(request, response); } catch (Throwable t) { log.log(Level.WARNING, t.getMessage(), t); } } } 

Please help me :) Thank you.

EDIT: here is the contents of my web.xml file

 <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0" metadata-complete="true"> <servlet> <servlet-name>jsonrpc</servlet-name> <servlet-class>assign1a.RPCServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>jsonrpc</servlet-name> <url-pattern>/rpc</url-pattern> </servlet-mapping> </web-app> 
+6
tomcat servlets servletexception
source share
3 answers

I noticed the phrase (wrong name: server/RPCServlet) in the error message.

This usually means that the class file has a different package name than the directory structure in jar / war.

Perhaps you originally put RPCServlet.java under a dir named server , and then moved RPCServlet.java to assign1a ?

Or maybe the source package is defined as:

 package server; 

And then you changed it later to

 package assign1a; 

In any case, in order to solve this problem, I suggest deleting all compiled files (class files, military files, etc.). Stop tomcat, and then delete the <TOMCAT_HOME>/work directory to make sure everything is gone.

Then check to make sure that RPCServlet.java is in the folder named assign1a and make sure the package is defined as:

 package assign1a; 

Recompile / rebuild the war and you should be in the best condition. Perhaps try to build a war in Eclipse (or Netbeans) because this will not allow you to compile to a folder that does not match the package name of the class files.

Good luck. - Dave

+4
source share

your error "noclassdeffound" means that tomcat cannot find assign1a.RPCServlet when necessary. so that tomcat can find the class in question, it must be in the path of the tomcat class. The best way to put it there is to make sure that it is in the classes folder. however, finding this folder can be a little difficult: there are several options, depending on which system you are on, how you are developing and how you are deploying.

start by looking in the tomcat installation folder under a folder called webapps. there, find the folder named [context], as in your URL http: // localhost: 8080 / [context] / RPC. you see? the folder should be named by the url you are using. (it would be easier to give accurate help if you provided accurate information, it’s bad not knowing the URL that you are using.) in this folder, find the WEB-INF folder, and there you should find the folder with the class names, in this the folder should be a folder named assign1a, and there you should have the RPCServlet.class file, which is a compiled file.

therefore, in the abstract short: [tomcat_install_folder] / webapps / [context] /WEB-INF/classes/assign1a/RPCServlet.class is missing. oh, and slashes are, of course, backslashes on windows :)

eclipse with dynamic web project

also, if you are developing in eclipse and using the tomcat server inside eclipse, your tomcat folder is actually located in a completely different place. you will find it under the eclipse workspace in the .metadata / .plugins / org.eclipse.wst.server.core / tmp0 / wtpwebapps / [context] folder, where tmp0 could be tmp1 or something else, depending on how much tomcat servers you started into the eclipse workspace.

if you still cannot understand, please write more information about your setup (i.e. development idea, deployment scenario, visit URL, web.xml)

+2
source share
 java.lang.NoClassDefFoundError: assign1a/RPCServlet (wrong name: server/RPCServlet) 

Do you see a folder named / assign 1a under / WEB-INF / classes in your WAR? And does it contain a file called RPCServlet.class? If not, you should compile your servlet.

Publish your web.xml and show the <servlet> and <servlet-mapping> entries.

0
source share

All Articles