Compile error: package javax.servlet does not exist

225,329

Solution 1

You need to add the path to Tomcat's /lib/servlet-api.jar file to the compile time classpath.

javac -cp .;/path/to/Tomcat/lib/servlet-api.jar com/example/MyServletClass.java

The classpath is where Java needs to look for imported dependencies. It will otherwise default to the current folder which is included as . in the above example. The ; is the path separator for Windows; if you're using an Unix based OS, then you need to use : instead.

If you're still facing the same complation error, and you're actually using Tomcat 10 or newer, then you should be migrating the imports in your source code from javax.* to jakarta.*.

import jakarta.servlet.*;
import jakarta.servlet.http.*;

In case you want to keep using javax.* for whatever reason, then you should be downgrading to Tomcat 9 or older as that was the latest version still using the old javax.* namespace.

See also:

Solution 2

If you are working with maven project, then add following dependency to your pom.xml

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
</dependency>

Solution 3

Is it a JSP or Servlet?

Well, these two packages aren’t actually built into Java like java.io is. Instead, they come with the Servlet-capable Web server (e.g. Tomcat). So before the Java compiler will be able to compile our Servlet, we need to let it know where to find the classes in these two packages.

The classes required are normally stored in a file called servlet.jar. The exact location of this file will depend on the particular Web server software you use, but in the case of Tomcat you can find it in the lib subdirectory of the main Tomcat installation directory (e.g. d:\Program Files\Apache Group\jakarta-tomcat-3.2.3\lib\servlet.jar). For the Java compiler to be able to compile Servlets, you need to add this file to your Java class path. By default, Java looks for classes in the current directory (".") only. Thus, "." is the default class path. If you change the class path to include the servlet.jar file (".;d:...\lib\servlet.jar" under Windows, ".:/usr/.../lib/servlet.jar" in Unix), then the Servlet should compile just fine.

You can specify a class path to use when you run javac.exe as follows:

d:\javadev> javac -classpath ".;d:\Program Files\Apache Group\ jakarta-tomcat-3.2.3\lib\servlet.jar" MyServlet.java

Or in Linux javac uses : instead of ;

server1> javac -classpath ".:./servlet/servlet.jar" MyServlet.java

Solution 4

In a linux environment the soft link apparently does not work. you must use the physical path. for instance on my machine I have a softlink at /usr/share/tomacat7/lib/servlet-api.jar and using this as my classpath argument led to a failed compile with the same error. instead I had to use /usr/share/java/tomcat-servlet-api-3.0.jar which is the file that the soft link pointed to.

Solution 5

This is what solved the problem for me:

<dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>jsp-api</artifactId>
    <version>2.2</version>
    <scope>provided</scope>
</dependency>
Share:
225,329
Karadous
Author by

Karadous

Updated on December 03, 2021

Comments

  • Karadous
    Karadous over 2 years

    I have a package in which I import javax.servlet.* and javax.servlet.http.* When I try to compile it in command prompt I get the error

    package javax.servlet does not exist

    I use JDK 1.7.0 and Tomcat 6.0.

  • Kris
    Kris over 12 years
    true, but in the future consider using maven to solve this kind of problems for you
  • BalusC
    BalusC over 12 years
    Or just an IDE like Eclipse. I'd however recommend continuing learning the hard way until you can almost dream it. Otherwise it will be hard to understand how IDEs work under the covers.
  • Karadous
    Karadous over 12 years
    should I do this anytime I compile a file? I mean is there a way to set classpath for all the time I compile a file.
  • Frankline
    Frankline over 12 years
    For you to compile the file i.e. *.java, you have to ensure the servlet.jar is in the classpath. Note that JSPs eventually get translated to servlets which are, ofcourse, Java files.
  • Pubby
    Pubby about 12 years
    Someone has the advice: "If you are using Windows Adding d:\Program Files\Apache Group\ jakarta-tomcat-3.2.3\lib\servlet.jar; to JAVA_HOME Variable also does the magic"
  • MarkHu
    MarkHu over 7 years
    The Gradle equivalent is dependencies { compile group: 'javax.servlet', name: 'servlet-api', version:'2.4' }
  • raksheetbhat
    raksheetbhat about 6 years
    Worked like a charm. Thanks.
  • Stu_Dent
    Stu_Dent almost 4 years
    My apology, if I have more than one servlet class, how can I add those to resolve this problem? thanks
  • BalusC
    BalusC almost 4 years