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

Thread Starter

BrettC

Joined Mar 19, 2022
9
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 .\WelcomeServlet.java
.\WelcomeServlet.java:15: error: cannot find symbol
public class WelcomeServlet extends HttpServlet {
^
symbol: class HttpServlet
.\WelcomeServlet.java:18: error: cannot find symbol
protected void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
^
symbol: class HttpServletRequest
location: class WelcomeServlet
import jakarta.servlet.*;
^
.\WelcomeServlet.java:12: error: package jakarta.servlet.http does not exist
import jakarta.servlet.http.*;
^
.\WelcomeServlet.java:17: error: method does not override or implement a method from a supertype
@override
^
7 errors


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

public class WelcomeServlet extends HttpServlet {
   // process "get" requests from clients
   [USER=54416]@override[/USER]
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("<html>");
      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]
 

xox

Joined Sep 8, 2017
702
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\*" find.java

Which here -cp tag stands for CLASSPATH Which compiles the program within the same directory as find.java 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
 
Top