Tomcat 10 on Mac not Recognizing servlet-api.jar when compiling servlet

707 Views Asked by At

I'm working on creating a Servlet for a tomcat webpage and the Servlet will not compile.

I'm using the servlet-api.jar & mysql-connector.8.0.28.jar for the servlet, and there are also several JavaBeans I have created to store information being passed between the database and the page.

The directory structure for tomcat is as follows:

  • all .jar files are in ./Tomcat/tomcat-10/lib/
  • the servlet to be compiled is stored at ./Tomcat/tomcat-10/webapps/CenterStage/WEB-INF/classes/
  • the beans are stored at ./Tomcat/tomcat-10/webapps/CenterStage/WEB-INF/classes/centerStage/

This is the command I use to compile everything:

javac -cp .:./Tomcat/tomcat-10/lib/servlet-api.jar:./Tomcat/tomcat-10/lib/mysql-connector-java-8.0.28.jar -classpath ./Tomcat/tomcat-10/webapps/CenterStage/WEB-INF/classes/:./Tomcat/tomcat-10/lib/ ./Tomcat/tomcat-10/webapps/CenterStage/WEB-INF/classes/CenterStageServlet.java

When I try to compile the servlet I get 9 Errors pointing to the HttpServlet class:

./Tomcat/tomcat-10/webapps/CenterStage/WEB-INF/classes/CenterStageServlet.java:32: error: cannot find symbol
public class CenterStageServlet extends HttpServlet {
                                        ^
  symbol: class HttpServlet
./Tomcat/tomcat-10/webapps/CenterStage/WEB-INF/classes/CenterStageServlet.java:25: error: cannot find symbol
@WebServlet(name = "CenterStageServlet", 
 ^
  symbol: class WebServlet
./Tomcat/tomcat-10/webapps/CenterStage/WEB-INF/classes/CenterStageServlet.java:41: error: cannot find symbol
   public void doGet(HttpServletRequest request, HttpServletResponse response)
                     ^
  symbol:   class HttpServletRequest
  location: class CenterStageServlet
./Tomcat/tomcat-10/webapps/CenterStage/WEB-INF/classes/CenterStageServlet.java:41: error: cannot find symbol
   public void doGet(HttpServletRequest request, HttpServletResponse response)
                                                 ^
  symbol:   class HttpServletResponse
  location: class CenterStageServlet
./Tomcat/tomcat-10/webapps/CenterStage/WEB-INF/classes/CenterStageServlet.java:42: error: cannot find symbol
         throws IOException, ServletException {
                             ^
  symbol:   class ServletException
  location: class CenterStageServlet
./Tomcat/tomcat-10/webapps/CenterStage/WEB-INF/classes/CenterStageServlet.java:6: error: package jakarta.servlet does not exist
import jakarta.servlet.*;             // Tomcat 10
^
./Tomcat/tomcat-10/webapps/CenterStage/WEB-INF/classes/CenterStageServlet.java:7: error: package jakarta.servlet.http does not exist
import jakarta.servlet.http.*;
^
./Tomcat/tomcat-10/webapps/CenterStage/WEB-INF/classes/CenterStageServlet.java:8: error: package jakarta.servlet.annotation does not exist
import jakarta.servlet.annotation.*;  // Tomcat 10
^
./Tomcat/tomcat-10/webapps/CenterStage/WEB-INF/classes/CenterStageServlet.java:40: error: method does not override or implement a method from a supertype
   @Override
   ^
9 errors

Servlet Code:

package centerStage;

// To save as "<TOMCAT_HOME>\webapps\CenterStage\WEB-INF\classes\CenterStageServlet.java"
import java.io.*;
import java.sql.*;
import jakarta.servlet.*;             // Tomcat 10
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;  // Tomcat 10
//import javax.servlet.*;             // Tomcat 9
//import javax.servlet.http.*;
//import javax.servlet.annotation.*;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.sql.Date;
import java.time.LocalDate;

import centerStage.*;
 
@WebServlet(name = "CenterStageServlet", 
            urlPatterns = {"/appointment", "/availibility", "/barber", "/client", "/service"})    
            // Configure the request URL for this servlet (Tomcat 7/Servlet 3.0 upwards)


public class CenterStageServlet extends HttpServlet {

    // Variables
    
   // The doGet() runs once per HTTP GET request to this servlet.
   @Override
   public void doGet(HttpServletRequest request, HttpServletResponse response)
         throws IOException, ServletException {
 
       // Do some processing
   }

   // Other Methods
}

I've verified that the .jar's that I'm looking for are in that /lib/ directory.

Compiling other Webapps in this Tomcat directory don't give me any issues, so I'm not sure what the problem is.

Any help/suggestions would be great!

1

There are 1 best solutions below

0
On

Okay, turns out it was something really simple.

When I moved the servlet from ~Tomcat/webapps/CenterStage/WEB-INF/classes/ to ~Tomcat/webapps/CenterStage/WEB-INF/classes/centerStage/ and then tried to compile with javac -cp .:~/Tomcat/lib/servlet-api.jar:~/Tomcat/lib/mysql-connector-java-8.0.28.jar ./Tomcat/webapps/CenterStage/WEB-INF/classes/centerStage/*.java everything worked fine.

So basically, I should have had the Servlet.java in the same directory as the JavaBeans and everything works fine.