Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile error: package javax.servlet does not exist

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.

like image 735
Karadous Avatar asked Feb 08 '12 12:02

Karadous


People also ask

How to solve javax servlet does not exist?

You need to add the path to Tomcat's /lib/servlet-api. jar file to the compile time classpath. The classpath is where Java needs to look for imported dependencies. It will otherwise default to the current folder which is included as .

Where is javax servlet package?

It contains, among others, the files /usr/share/java/servlet-api-2.5. jar and /usr/share/java/jsp-api-2.1. jar , which are the servlet and JSP libraries you need.

What is API servlet?

It defines an object to dispatch the request and response to any other resource, means it receives requests from the client and sends them to a servlet/HTML file/JSP file on the server. Servlet. This is the main interface that defines the methods in which all the servlets must implement.

What are the two interfaces in the servlet API?

Servlets use classes and interfaces from two packages: javax. servlet and javax. servlet.


9 Answers

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?
like image 65
BalusC Avatar answered Oct 11 '22 22:10

BalusC


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>
like image 21
Swalih Avatar answered Oct 11 '22 22:10

Swalih


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

like image 25
Frankline Avatar answered Oct 12 '22 00:10

Frankline


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.

like image 30
Chezzwizz Avatar answered Oct 12 '22 00:10

Chezzwizz


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>
like image 37
Koray Tugay Avatar answered Oct 12 '22 00:10

Koray Tugay


Copy the file "servlet-api.jar" from location YOUR_INSTILLATION_PATH\tomcat\lib\servlet-api.jar and Paste the file into your Java Directory YOUR_INSTILLATION_PATH\Java\jdk1.8.0_121\jre\lib\ext

this will work (tested).

like image 42
Md Rehan Avatar answered Oct 12 '22 00:10

Md Rehan


Add servlet-api.jar into your classpath. It will be available into Tomcat's lib folder.

like image 25
Abhendra Singh Avatar answered Oct 11 '22 22:10

Abhendra Singh


JSP and Servlet are Server side Programming. As it comes as an built in package inside a Server like Tomcat. The path may be like wise

C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\jsp-api.jar
C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\servlet-api.jar

Just you want to Do is Add this in the following way

Right Click> My Computer>Advanced>Environment Variables>System variables

Do> New..> Variable name:CLASSPATH
           Variable value:CLASSPATH=.;C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\servlet-api.jar;
like image 21
Ravi Chandran Avatar answered Oct 11 '22 22:10

Ravi Chandran


This happens because java does not provide with Servlet-api.jar to import directly, so you need to import it externally like from Tomcat , for this we need to provide the classpath of lib folder from which we will be importing the Servlet and it's related Classes.

For Windows you can apply this method:

  1. open command prompt
  2. type
 javac -classpath "C:\Program Files\Apache Software Foundation\Tomcat 9.0\lib\*;" YourFileName.java 
     
  1. It will take all jar files which needed for importing Servlet, HttpServlet ,etc and compile your java file.

  2. You can add multiple classpaths Eg.

javac -classpath "C:\Users\Project1\WEB-INF\lib\*; C:\Program Files\Apache Software Foundation\Tomcat 9.0\lib\*;" YourFileName.java
like image 41
Prasad Kadu Avatar answered Oct 11 '22 23:10

Prasad Kadu