简体   繁体   English

编译错误:包 javax.servlet 不存在

[英]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我有一个包,我在其中导入 javax.servlet.* 和 javax.servlet.http.* 当我尝试在命令提示符下编译它时出现错误

package javax.servlet does not exist包 javax.servlet 不存在

I use JDK 1.7.0 and Tomcat 6.0.我使用 JDK 1.7.0 和 Tomcat 6.0。

You need to add the path to Tomcat's /lib/servlet-api.jar file to the compile time classpath.您需要将 Tomcat 的/lib/servlet-api.jar文件的路径添加到编译时类路径中。

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

The classpath is where Java needs to look for imported dependencies.类路径是 Java 需要查找导入依赖项的地方。 It will otherwise default to the current folder which is included as .否则,它将默认为包含为. in the above example.在上面的例子中。 The ; ; is the path separator for Windows;是 Windows 的路径分隔符; if you're using an Unix based OS, then you need to use : instead.如果您使用的是基于 Unix 的操作系统,则需要使用:代替。

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.* .如果您仍然面临相同的编译错误,并且您实际上使用的是 Tomcat 10 或更新版本,那么您应该将源代码中的导入从javax.*迁移到jakarta.*

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

See also:也可以看看:

If you are working with maven project, then add following dependency to your pom.xml如果您正在使用 maven 项目,则将以下依赖项添加到您的 pom.xml

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

Is it a JSP or Servlet?它是 JSP 还是 Servlet?

Well, these two packages aren't actually built into Java like java.io is.嗯,这两个包实际上并没有像 java.io 那样内置在 Java 中。 Instead, they come with the Servlet-capable Web server (eg Tomcat).相反,它们带有支持 Servlet 的 Web 服务器(例如 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.因此,在 Java 编译器能够编译我们的 Servlet 之前,我们需要让它知道在哪里可以找到这两个包中的类。

The classes required are normally stored in a file called servlet.jar.所需的类通常存储在名为 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 (eg d:\\Program Files\\Apache Group\\jakarta-tomcat-3.2.3\\lib\\servlet.jar).该文件的确切位置取决于您使用的特定 Web 服务器软件,但对于 Tomcat,您可以在 Tomcat 主安装目录的 lib 子目录中找到它(例如 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.为了让 Java 编译器能够编译 Servlet,您需要将此文件添加到 Java 类路径中。 By default, Java looks for classes in the current directory (".") only.默认情况下,Java 仅在当前目录(“.”)中查找类。 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.如果更改类路径以包含 servlet.jar 文件(Windows 下为“.;d:...\\lib\\servlet.jar”,Unix 下为“.:/usr/.../lib/servlet.jar” ),那么 Servlet 应该可以正常编译。

You can specify a class path to use when you run javac.exe as follows:您可以指定在运行 javac.exe 时使用的类路径,如下所示:

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 ;或者在 Linux javac 中使用 : 而不是 ;

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

In a linux environment the soft link apparently does not work.在 linux 环境中,软链接显然不起作用。 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.例如,在我的机器上,我在/usr/share/tomacat7/lib/servlet-api.jar有一个软链接,使用它作为我的类路径参数导致编译失败并出现相同的错误。 instead I had to use /usr/share/java/tomcat-servlet-api-3.0.jar which is the file that the soft link pointed to.相反,我不得不使用/usr/share/java/tomcat-servlet-api-3.0.jar这是软链接指向的文件。

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>

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从位置YOUR_INSTILLATION_PATH\\tomcat\\lib\\servlet-api.jar复制文件“ servlet-api.jar ”并将该文件粘贴到您的 Java 目录中YOUR_INSTILLATION_PATH\\Java\\jdk1.8.0_121\\jre\\lib\\ext

this will work (tested).这将工作(测试)。

JSP and Servlet are Server side Programming. JSP 和 Servlet 是服务器端编程。 As it comes as an built in package inside a Server like Tomcat.因为它是像 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;

Add servlet-api.jar into your classpath.将 servlet-api.jar 添加到您的类路径中。 It will be available into Tomcat's lib folder.它将在 Tomcat 的 lib 文件夹中可用。

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.在我的环境变量中添加 /usr/local/apache-tomcat-7.0.64/lib/servlet-api.jar 作为 Mac 上的 CLASSPATH。

if using bash: ~/.bash_profile $CLASSPATH=/usr/local/apache-tomcat-7.0.64/lib/servlet-api.jar如果使用 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"如果使用 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.立即强制它工作,运行source .bash_profile (或 .zshrc) 或者可以重新启动计算机,它适用于当前用户。

Even after trying suggested solution, it was not solving my problem because there where many instance of java path were entered by me.即使在尝试了建议的解决方案之后,它也没有解决我的问题,因为在那里我输入了许多 java 路径实例。

  1. I removed all java related path (different version java) from "Path, JAVA_HOME, JRE_HOME" and created from fresh.我从“路径,JAVA_HOME,JRE_HOME”中删除了所有与java相关的路径(不同版本的java)并从新创建。

  2. i have set (path may changes as per different installation)我已经设置(路径可能会根据不同的安装而改变)
    a.一种。 JAVA_HOME as C:\\Program Files\\Java\\jdk1.8.0_191 JAVA_HOME 为 C:\\Program Files\\Java\\jdk1.8.0_191
    b.JRE_HOME as C:\\Program Files\\Java\\jdk1.8.0_191\\jre\\lib JRE_HOME 为 C:\\Program Files\\Java\\jdk1.8.0_191\\jre\\lib
    c. C。 add binary file path in path: C:\\Program Files\\Java\\jdk1.8.0_191\\bin在路径中添加二进制文件路径:C:\\Program Files\\Java\\jdk1.8.0_191\\bin
    d. d. CLASSPATH as C:\\apache-tomcat-7.0.93\\lib类路径为 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:参考图片: 在此处输入图片说明

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.发生这种情况是因为java没有提供Servlet-api.jar直接导入,所以你需要像从Tomcat一样从外部导入它,为此我们需要提供我们将从中导入Servlet的lib文件夹的类路径及其相关班级。

For Windows you can apply this method:对于 Windows,您可以应用此方法:

  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.它将获取导入 Servlet、HttpServlet 等所需的所有 jar 文件并编译您的 java 文件。

  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

http://www.java2s.com/Code/JarDownload/javax.servlet/javax.servlet.jar.zip 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从该位置下载zip文件,解压后放入如下路径

%JAVA_HOME%/jre/lib/ext/ %JAVA_HOME%/jre/lib/ext/

place files in the without any directory将文件放在没有任何目录

The possible solution (Tested on ubuntu )可能的解决方案(在ubuntu 上测试)

  1. Open Terminal type geany .bashrc打开终端类型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.现在尝试运行该程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM