How to resolve an error: cannot find symbol for HttpServlet?

Thread Starter


Joined Mar 19, 2022
I am trying to test out a simple web application to make sure Tomcat is working properly. I am tying to compile this java file in VSCode but I am receiving some errors:

PS C:\Program Files\Apache Software Foundation\Tomcat 10.0_Tomcat10022\webapps\first-example\WEB-INF\classes> javac .\
.\ error: cannot find symbol
public class WelcomeServlet extends HttpServlet {
symbol: class HttpServlet
.\ error: cannot find symbol
protected void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
symbol: class HttpServletRequest
location: class WelcomeServlet
import jakarta.servlet.*;
.\ error: package jakarta.servlet.http does not exist
import jakarta.servlet.http.*;
.\ error: method does not override or implement a method from a supertype
7 errors

This is the java file in question:
import jakarta.servlet.*;
import jakarta.servlet.http.*;

public class WelcomeServlet extends HttpServlet {
   // process "get" requests from clients
protected void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException  {

      response.setContentType( "text/html" );
      PrintWriter out = response.getWriter();
      // send HTML5 page to client
      // start HTML5 document
      out.println( "<meta charset=\"utf-8\">" );
           // head section of document
      out.println( "<head>" );
      out.println( "<style type='text/css'>");
      out.println( "<!--  body{background-color:blue; color:white; font-family: Verdana, Arial, sans-serif; text-align: center;}");
      out.println( " h1{font-size:100pt; text-align:center;} h2{font-family:inherit; font-size:60pt;}");
      out.println( " #one{color:magenta;} #two{color:yellow;} #three{color:red;} #four{color:lime;} #five{color:cyan;}");
      out.println( "-->");
      out.println( "</style>");
      out.println( "<title>Welcome to Servlets!</title>" );
      out.println( "</head>" );
      // body section of document
      out.println( "<body>" );
      out.println( "<h1><span id=\"one\">H</span><span id=\"two\">e</span><span id=\"three\">l</span>"
            + "<span id=\"four\">l</span><span id=\"five\">o</span>!!</h1>");
      out.println( "<h2>Welcome To The Exciting World Of Servlet Technology!</h2>" );
      out.println( "</body>" );
      // end HTML5 document
      out.println( "</html>" );
      out.close();  // close stream to complete the page
   } //end doGet() method

public static void main(String[] args) {
} //end WelcomeServlet class
I have made sure that I am referencing the servlet-api.jar file in the lib directory. I have tried to use import.javax.servlet.http.* instead, but am getting an error. Any suggestions on how to resolve these errors would be helpful.

Moderator edit: Enclosed program code in code tags like this:
[code=java] ... your code ... [/code]


Joined Sep 8, 2017
IIRC javac can pretty fussy if the "classpath" isn't specified or otherwise defined.

javac –classpath C:\3rdParty\Somelibrary.jar MyProgram.Java
java –classpath C:\3rdParty\Somelibrary.jar MyProgram
You can also set it globally via the CLASSPATH environment variable.
So My Guess about it not finding the required libraries wore right. But other than using

set CLASSPATH="C:\Program Files\Apache Software Foundation\Tomcat 9.0\lib\*":%CLASSPATH%

I used:

javac -cp "C:\Program Files\Apache Software Foundation\Tomcat 9.0\lib\*"

Which here -cp tag stands for CLASSPATH Which compiles the program within the same directory as is located.
"Class Path Wild Cards" section it explains * is "considered equivalent to specifying a list of all of the files in the directory with the extension .jar or .JAR". I am not JEE dev so I am not sure about location of proper libraries, but maybe problem is that OP should use only * not *.jar. All Savers Provider Portal