Can't run my servlet from tomcat server even though the classes are in package

Mido picture Mido · Jan 7, 2011 · Viewed 28.5k times · Source

i am trying to get my servlet to run, i have been searching for 2 days and trying every possible solution and no luck. The servet class is in the appropriate folder (i.e under the package name). I also added the jar files needed in my servlet into lib folder. the web.xml file maps the url and defines the servlet. So i did everything in the documentation and wt people said in here and still getting 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 :) Thanks.

EDIT: here are 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>

Answer

Upgradingdave picture Upgradingdave · Jan 7, 2011

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 the jar/war.

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

Or maybe the original package defined as:

package server;

And then you changed it later to

package assign1a;

Either way, to solve it, I suggest that you delete all the compiled stuff (class files, war files, etc). Stop tomcat, then delete the <TOMCAT_HOME>/work directory just to be sure everything is gone.

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

package assign1a;

Recompile/rebuild war and you should be in a better state. Maybe try building the war in Eclipse (or Netbeans) because that way it won't let you compile into a folder that doesn't match the class files package name.

Good Luck. - Dave