Home > Blockchain >  java.lang.ClassNotFoundException with Tomcat WAR file
java.lang.ClassNotFoundException with Tomcat WAR file

Time:11-18

I'm attempting to make a simple REST API for a project I'm working on currently to simplify a task I have to do every day, and when I get to where I'm hosting it I'm met with a 500 Server Error. Looking in the logs in (the tomcat home dir)/logs/localhost.2021-10-22.log, I found this error:

22-Oct-2021 21:33:58.974 SEVERE [http-nio-13760-exec-64] org.apache.catalina.core.StandardWrapperValve.invoke Allocate exception for servlet [OrderServlet]
java.lang.ClassNotFoundException: api.OrderServlet
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1407)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1215)
    at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:538)
    at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:519)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:149)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1070)
    at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:788)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:128)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:540)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:687)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:357)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:382)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:895)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1722)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
    at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

I also have the code for the class that "doesn't exist" here:

package api;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import data.Order;

public class OrderServlet extends HttpServlet {
    
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        
        String requestUrl = request.getRequestURI();
        String id = requestUrl.substring("cafe/orders/".length());
        
        
        Order order = Requests.getInstance().getOrder(id);
        
        if (order != null) {
            String json = "{\n";
            json  = "\"id\": "   JSONObject.quote(order.getID())   ",\n";
            json  = "\"id\": "   JSONObject.quote(order.getName())   ",\n";
            json  = "\"id\": "   JSONObject.quote(String.valueOf(order.getTime()))   ",\n";
            //various parameters removed to prevent abuse in the future (I'm looking at you, Dax)
            //I'd rather not have a coffee order show up with a comically large amount of creamers in it
            json  = "};";
            response.getOutputStream().println(json);
        } else {
            //order was not found
            response.sendError(404);
        }
    }
    
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse responce) throws IOException, ServletException {
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        int time = Integer.parseInt(request.getParameter("time"));
        //various parameters removed
        
        Requests.getInstance().putOrder(new Order(id, name, time, type, //various parameters removed
));
    }

}

I've checked that the class actually exists, I've tried rebuilding the classes, importing as non-compiled files and messing around with web.xml, and I've checked that there is not a random jre6 library in the build path (from here). I am not using Maven, and Tomcat is able to load docs and manager just fine. How do I go about making the class recognizable by Tomcat?

CodePudding user response:

Looks like the file didn't export correctly from Eclipse, making it manually fixed the problem.

EDIT: Turns out the problem was that my source folder was not the folder Eclipse was attempting to export. Going into Properties > Deployment assembly and changing the source folder fixed it (Thanks @Piotr P Karwasz!)

  • Related