package javax.servlet.http does not exist [duplicate]
I have the jre7 and jdk1.7.0 installed along with the Tomcat 7.0 but it shows this error. servlet.http is not the only one that "does not exist", there are also other (servlet.) components.
Can anybody help me with this?
UPDATE: This error occurs when I try the follow command: javac -classpath servlet-api.jar WebTest.java
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>
If you're using the command console to compile the servlet, then you should include Tomcat's /lib/servlet-api.jar
in the compile classpath.
javac -cp .:/path/to/tomcat/lib/servlet-api.jar com/example/MyServlet.java
(use ;
instead of :
as path separator in Windows)
If you're using an IDE, then you should integrate Tomcat in the IDE and reference it as target runtime in the project. If you're using Eclipse as IDE, see also this for more detail: How do I import the javax.servlet API in my Eclipse project?
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.
On *nix, try:
javac -cp $CLASSPATH:$CATALINA_HOME/lib/servlet-api.jar Filename.java
Or on Windows, try:
javac -cp %CLASSPATH%;%CATALINA_HOME%\lib\servlet-api.jar Filename.java
If you are using Ant and trying to build then you need to :
Specify tomcat location by
<property name="tomcat-home" value="C:\xampp\tomcat" />
Add tomcat libs to your already defined path for jars by
<path id="libs"> <fileset includes="*.jar" dir="${WEB-INF}/lib" /> <fileset includes="*.jar" dir="${tomcat-home}/bin" /> <fileset includes="*.jar" dir="${tomcat-home}/lib" /> </path>
This error occurs when you compile a java program using classes that support the Servlet API. The compiler searches for the library (included in a .jar file) by using the CLASSPATH. You can specify this when you compile using -classpath
or -cp
options as noted in other responses, but you should set up your environment to define the classpath as needed.
Set the CLASSPATH environment variable to reference the location of servlet-api.jar
, which depends on your setup (OS, how you installed, etc.)
Assuming you're using Tomcat and have installed it in one of 20 possible ways, the APIs used by servlets will be installed on your system, relative to wherever Tomcat is installed. For historical reasons, Tomcat is also known as "Catalina", so you can use the command "catalina" to run certain commands, and alone, it will report, amongst other things the CATALINA_BASE
. For example on my Mac using Tomcat installed using homebrew it's
Using CATALINA_BASE: /usr/local/Cellar/tomcat/8.5.9/libexec
The location of the Tomcat servlet libraries is under this in the lib
directory.
Set CATALINA_BASE, then set CLASSPATH using the base as a start, for example for Linux or OSX you might set this in .profile
, or .bash_profile
like so:
export CATALINA_BASE=/usr/local/Cellar/tomcat/8.5.9/libexec
export CLASSPATH=$CATALINA_BASE/lib/servlet-api.jar:$CLASSPATH
Exit the terminal/shell and come back in to run the profile. You should be able to see that the variable is set by using the echo
command, e.g.
echo $CLASSPATH
or in Windows
echo %CLASSPATH%
If it displays the full path to the jar `javac WebTest.java' compile your class.
Other answers are correct -- set up your IDE (Eclipse, IntelliJ) to know about Tomcat or build with Maven and you'll save pain.
The solution that work for is were add the next dependency to my pom.xml file.
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
If you use elcipse with tomcat server, you can open setting properties by right click project -> choose Properties (or Alt+Enter), continue do same below picture. It will resolve your problem.
Try:
javac -cp .;"C:\Users\User Name\Tomcat\apache-tomcat-7.0.108\lib\servlet-api.jar" HelloServlet.java
using windows if there are spaces in your class path.
You must add the classpath
for compile. In tomcat
classpath="C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\servlet-api.jar".
So the command is
javac -classpath "c:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\servlet-api.jar" yourfile.java .
Got this error on linux because of weird file permissions in tomcat distro. Some files an directories are not readable for other users. I use separate CATALINA_HOME and CATALINA_BASE, so my tomcat is owned by root and is runned by restricted user.
Fixing it like that:
( cd /usr/local/share/tomcat9/ && for file in `find ./ -type d ! -perm -o=r`; do echo "$file"; chmod o+rx "$file"; done && for file in `find ./ ! -perm -o=r`; do echo "$file"; chmod o+r "$file"; done )
for file in `find /usr/local/share/tomcat9/ ! -perm -o=x -name '*.sh'`; do echo "$file"; chmod o+x "$file"; done
All you gotta do is download the servlet-api.jar package. load the package and reset the reset the virtual machine
精彩评论