Compile Error: Package Javax.Servlet Does Not Exist

Compile error: package javax.servlet does not exist

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:

  • jakarta.servlet.ServletException: Class [com.practice.MyServlet] is not a Servlet
  • How do I import the javax.servlet / jakarta.servlet API in my Eclipse project?
  • What exactly is Java EE?

Compile time error: package javax.servlet does not exist import javax.servlet.*

Tomcat 10 is currently a milestone build, so not really good for first steps or meaningful development work, unless you specifically want to go after the root cause of your problem:

Tomcat 10 is not backwards compatible as the whole Java EE (or whatever label it flew under) is now replaced with the new Jakarta EE, thus your import javax.servlet.* now needs to be import jakarta.servlet.*.

Instead of adapting everything right now, you'll probably be happier by just utilizing Tomcat 9 and go with the old and (documented everywhere) javax.servlet.*

Package javax.servlet.*; does not exist, other javax package also not exist

I tested your example and the compiler error was shown when you have mispelling in the path.

Command:

javac -classpath "D:\tomcatX\lib\servlet-api.jar;classes;" -d classes src\Ch1Servlet.java

Error:

src\Ch1Servlet.java:3: package javax.servlet.http does not exist
import javax.servlet.http.HttpServlet;

Command:

javac -classpath "D:\tomcat\lib\servlet-api.jar;classes;" -d classes src\Ch1Servlet.java

Now it is ok.

So check carefully your path to the Tomcat (I think than it is lack of some spaces):

javac -classpath "C:\ProgramFiles\ApacheSoftwareFoundation\Tomcat7.0\lib\servlet-api.jar;classes;‌​" -d classes src\Ch1Servlet.java

Probably it should be:

javac -classpath "C:\Program Files\Apache Software Foundation\Tomcat7.0\lib\servlet-api.jar;classes;‌​" -d classes src\Ch1Servlet.java

error: package javax.servlet does not exist

The javax.servlet dependency is missing in your pom.xml. Add the following to the dependencies-Node:

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

package javax.servlet.http does not exist

Your CLASSPATH variable does not point to the directory containing the javax classes. The CLASSPATH variable specifies where the java compiler should look for java class file resources. If it does not know to look in the javax directory, then it will never find the file(s) you are after.

package javax.servlet does not exist although the package is found by vsCode

In case the source files reference third-party library (JAR) files, use the -cp option and specify the JAR file. For example:

javac -cp lib\mysql-connector-java-8.0.27.jar -d classes src\TestDb.java

If there are multiple JAR files, they must be separated by semicolon like this:

javac -cp lib\mysql-connector-java-8.0.27.jar;lib\log4j-1.2.17.jar -d classes src\TestDb.java

Error: package javax.servlet.http does not exist even after adding the CLASSPATH variable

It was a mistake on my part.

This was how i created my CLASSPATH through the command line

 export CLASSPATH=/home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/*

The directory I created was not ApacheTomcat, but it was Apache Tomcat(with a space in between).

Maybe that is why the CLASSPATH variable couldn't recognize the correct location.

I changed the directory name into ApacheTomcat(removed the space).

Now i defined the CLASSPATH variable in the .bashrc file as follows:

#classpath
export CLASSPATH=/home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/*

Now when i do echo${CLASSPATH}, I get all the jar files which i mentioned.

nithinchandranp@b-40:~$ echo ${CLASSPATH}
/home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/annotations-api.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/catalina-ant.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/catalina-ha.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/catalina.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/catalina-storeconfig.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/catalina-tribes.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/ecj-4.6.3.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/el-api.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/jasper-el.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/jasper.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/jaspic-api.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/jsp-api.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/servlet-api.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/tomcat-api.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/tomcat-coyote.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/tomcat-dbcp.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/tomcat-i18n-es.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/tomcat-i18n-fr.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/tomcat-i18n-ja.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/tomcat-i18n-ru.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/tomcat-jdbc.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/tomcat-jni.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/tomcat-util.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/tomcat-util-scan.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/tomcat-websocket.jar /home/nithinchandranp/ApacheTomcat/apache-tomcat-8.5.34/lib/websocket-api.jar


Related Topics



Leave a reply



Submit