开发者

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

开发者 https://www.devze.com 2023-02-03 12:11 出处:网络
i am trying to get my servlet to run, i have been searching for 2 days and trying every possible solution and no luck.

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>


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


your error "noclassdeffound" means that tomcat can't find assign1a.RPCServlet when it needs it. in order for tomcat to find the class in question, it must be on tomcat's class path. the best way of putting it there would be to make sure it's in the classes-folder. however, finding this folder can be a bit tricky D: there are several possibilities, depending on what system you're on, how you're developing, and how you're deploying.

start by looking under your tomcat installation folder, under a folder named webapps. in there, find a folder named [context] as in your url http://localhost:8080/[context]/rpc. you see? the folder must be named as the url you're using. (it would be easier to give exact help, if you had provided exact information, it's dificult without knowing the url you're using.) in this folder, find the folder WEB-INF, and there, you should find a folder named classes. under this folder you must have a folder named assign1a, and in there you must have the file RPCServlet.class, which is the compiled file.

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

eclipse with dynamic web project

also, if you're developing in eclipse, and are using a tomcat server inside eclipse, then your tomcat folder is actually in a totally different location. you then find it under your eclipse workspace, in the folder .metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/[context] where tmp0 could be tmp1 or something else, depending on how many tomcat servers you have started up under your eclipse workspace.

if you still can't figure it out, please post more info about your setup (i.e. develoment ide, deployment scenario, visiting url, web.xml)


java.lang.NoClassDefFoundError: assign1a/RPCServlet (wrong name: server/RPCServlet)

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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号