0

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?

N3ther
  • 76
  • 1
  • 10
  • 2
    Did you look in the war file and saw the class was there? – Robert Moskal Oct 23 '21 at 03:26
  • 2
    The [WAR](https://en.wikipedia.org/wiki/WAR_(file_format)) file is just a [ZIP](https://en.wikipedia.org/wiki/ZIP_(file_format)) file. You can unzip it, to examine contents. – Basil Bourque Oct 23 '21 at 03:45
  • 1
    Completely forgot I could just unzip it, looks like it didn't put _anything_ inside of it, it's just a blank file with the structure of a war file. Looks like I'm going to have to make it manually instead of exporting it from Eclipse. – N3ther Oct 23 '21 at 04:17

1 Answers1

0

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!)

N3ther
  • 76
  • 1
  • 10
  • 1
    Did you check the _"Deployment assembly"_ of your Eclipse project? It should contain your source folder and map it to `/WEB-INF/classes`. – Piotr P. Karwasz Oct 23 '21 at 05:32
  • @PiotrP.Karwasz that was it! Turns out it wasn't actually using the source folder to put into `/WEB-INF/classes`, fixing that made the .war file work! – N3ther Oct 23 '21 at 15:38