-1
javac -classpath "/opt/tomcat/lib/servlet-api.jar" /opt/tomcat/webapps/ROOT/WEB-INF/classes/NewServlet.java

I have tried replacing the javax.servlet with "jakarta.servlet". It still throws the same error with "package jakarta.servlet" does not exist.

I have included the servlet-api.jar while compiling the program. Nothing seems to work here. I am stuck with this for 3 days now. I am a newbie to the servlet. I am not using any IDE. My tomcat is running fine and it is showing the results but the class with these packages are not getting compiled and hence unable to process the following page.

I am using tomcat 10 and linux mint 20.1 .

3 Answers3

0

/WEB-INF/classes/ is not a recommended place to place .java source code (text code files), this is a folder for compiled Java .class (binary jvm executable files)

If that is not the error cause, notice this kind of error exist in other questions. General recommendation, search them before asking.

My recommendation is to try out maven. How to compile a servlet for Tomcat in command line? error: package javax.servlet does not exist

Non maven replies to similar questions:

Compiling servlets with javac

Javac erroring out when compiling Servlet libraries

Best regards.

João
  • 2,296
  • 5
  • 20
  • 30
-1

There is a possibility the jar file you included does not contain jakarta.servlet.

Download jarkarta servlet api JAR 5.0.0 from the link below and add to your CLASSPATH. It should work.

https://jar-download.com/artifacts/jakarta.servlet/jakarta.servlet-api/5.0.0-M2/source-code

  • 1
    Assuming a work tomcat installation, tomcat/lib/* always has servlet API in it, it does not make sense to download another. – João Feb 15 '22 at 00:58
-1

make sure that you have made a class public. ex.

public class ClassName extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
        // your Code   
    }
}
Kai-Sheng Yang
  • 1,535
  • 4
  • 15
  • 21
Rohan
  • 1