javac command line compile error: package javax.servlet does not exist
Asked Answered
A

14

80

I have a Servlet class 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. I compile using javac. I am not using a build tool like Maven.

Accepted answered 8/2, 2012 at 12:22 Comment(0)
H
106

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:

Heterosis answered 8/2, 2012 at 12:26 Comment(4)
true, but in the future consider using maven to solve this kind of problems for youVolatilize
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.Heterosis
My apology, if I have more than one servlet class, how can I add those to resolve this problem? thanksIncreate
@Stu_Dent: how to compile multiple files using javacHeterosis
Z
43

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>
Zolly answered 9/11, 2015 at 5:11 Comment(2)
The Gradle equivalent is dependencies { compile group: 'javax.servlet', name: 'servlet-api', version:'2.4' }Lysimachus
Worked like a charm. Thanks.Deflagrate
M
9

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

Mathia answered 8/2, 2012 at 12:29 Comment(3)
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.Accepted
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.Mathia
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"Southworth
R
5

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.

Romy answered 27/5, 2013 at 17:53 Comment(0)
C
5

Now that we are transitioning to from javax to jakarta namespace. I added a maven dependency

        <dependency>
        <groupId>jakarta.servlet</groupId>
        <artifactId>jakarta.servlet-api</artifactId>
        <version>6.0.0</version>
        <scope>provided</scope>
    </dependency>

and then edited the file

nbfs://nbhost/SystemFileSystem/Templates/JSP_Servlet/ServletListener.java

by changing the import

import javax.servlet.annotation.WebListener;

to

import jakarta.servlet.annotation.WebListener; now all my listener classes are created pre-jakartified.

I hope that helps cheers!

Chops answered 21/2, 2023 at 18:46 Comment(1)
Yep, this is the right answer. Took me like the whole day to figure it out but it is because javax is being replaced with jakarta. More details here if anyone's curious.Blabber
A
4

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>
Ancilin answered 5/8, 2016 at 17:2 Comment(0)
C
4

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

Chiekochien answered 15/4, 2018 at 12:15 Comment(0)
K
1

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

Korea answered 8/2, 2012 at 12:38 Comment(0)
N
1

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;
Navar answered 3/4, 2013 at 6:8 Comment(0)
K
1

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
Kochi answered 29/6, 2020 at 17:33 Comment(0)
C
0

This is what I found. Adding /usr/local/apache-tomcat-7.0.64/lib/servlet-api.jar in my environment variable as CLASSPATH on Mac.

if using bash: ~/.bash_profile $CLASSPATH=/usr/local/apache-tomcat-7.0.64/lib/servlet-api.jar

if using zsh: ~/.zshrc export CLASSPATH="usr/local/apache-tomcat-7.0.64/lib/servlet-api.jar"

Force it work right now, run source .bash_profile (or .zshrc) or one can restart computer and it works for the current user.

Crinkleroot answered 29/7, 2016 at 14:27 Comment(0)
Q
0

Even after trying suggested solution, it was not solving my problem because there where many instance of java path were entered by me.

  1. I removed all java related path (different version java) from "Path, JAVA_HOME, JRE_HOME" and created from fresh.

  2. i have set (path may changes as per different installation)
    a. JAVA_HOME as C:\Program Files\Java\jdk1.8.0_191
    b. JRE_HOME as C:\Program Files\Java\jdk1.8.0_191\jre\lib
    c. add binary file path in path: C:\Program Files\Java\jdk1.8.0_191\bin
    d. CLASSPATH as C:\apache-tomcat-7.0.93\lib

  3. never try in the same command prompt if its already open while doing changes/creting system/user variables. close it and open new one.

Reference Image: enter image description here

Quillet answered 19/3, 2019 at 2:50 Comment(0)
V
0

http://www.java2s.com/Code/JarDownload/javax.servlet/javax.servlet.jar.zip

Download zip file from the location and place it into following path after unziping files

%JAVA_HOME%/jre/lib/ext/

place files in the without any directory

Vulnerable answered 9/4, 2021 at 5:28 Comment(0)
T
-2

The possible solution (Tested on ubuntu)

  1. Open Terminal type geany .bashrc
  2. Go to the top and paste this
    export CLASSPATH=$CLASSPATH:/web/apache-tomcat-8.5.39/lib/servlet-api.jar
  3. Now save and close
  4. Try running the program now.
Tjader answered 7/4, 2019 at 16:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.