'package javax.servlet does not exist | package jakarta.servlet does not exist

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 .



Solution 1:[1]

/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.

Solution 2:[2]

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   
    }
}

Solution 3:[3]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 João
Solution 2 Kai-Sheng Yang
Solution 3 Collins Onah