java 包 javax.servlet.http 不存在但 servlet-api.jar 存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10624002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:53:01  来源:igfitidea点击:

package javax.servlet.http does not exist but servlet-api.jar does

javaservletsclasspath

提问by ggkmath

I've got my GlassFish 3.1.2 web server working fine with Java SE 1.6 for regular Java POJOs. The JAVA_HOME, JAVA_PATH, and JRE_HOME variables are set correctly. I'm using Linux CentOS6.2 and I'm doing all the java work from vi and the command line (e.g. no frameworks or tools like Spring, Eclipse, etc.).

我的 GlassFish 3.1.2 Web 服务器在 Java SE 1.6 上正常工作,用于常规 Java POJO。JAVA_HOME、JAVA_PATH 和 JRE_HOME 变量设置正确。我正在使用 Linux CentOS6.2,并且正在通过 vi 和命令行执行所有 Java 工作(例如,没有框架或工具,如 Spring、Eclipse 等)。

Now I want to use a Java Servlet, and I'm getting errors such as:

现在我想使用 Java Servlet,但出现以下错误:

package javax.servlet.http does not exist

I assumed that since my environment paths are set correctly, that the servlet files would be visible to during compile. Apparently this isn't the case.

我假设由于我的环境路径设置正确,servlet 文件在编译期间是可见的。显然情况并非如此。

Anyone know how to fix this error? If were to hunt around on my linux directories, what filename am I looking to find for the servlet jar file? Anyone know where this file is located starting from JAVA_HOME for JDK 1.6?

有谁知道如何解决这个错误?如果要搜索我的 linux 目录,我要为 servlet jar 文件寻找什么文件名?任何人都知道这个文件从 JDK 1.6 的 JAVA_HOME 开始的位置?

Or, are the servlet-related files not part of jdk 1.6, and need to be downloaded installed separately?

或者,servlet相关文件不是jdk 1.6的一部分,需要单独下载安装吗?

UPDATE:

更新:

If I type locate servlet-api.jarfrom my linux prompt, it returns a path inside my GlassFish installation directories (e.g. ~/glassfish3/glassfish/modules/javax.servlet-api.jar). Thus, as mentioned below, it's part of the GlassFish installation.

如果我locate servlet-api.jar从我的 linux 提示符下键入,它会返回我的 GlassFish 安装目录中的路径(例如~/glassfish3/glassfish/modules/javax.servlet-api.jar)。因此,如下所述,它是 GlassFish 安装的一部分。

回答by Jigar Joshi

You need servlet-apiin your classpath when it is being compiled, it might be there with server but it would be available at runtime (not at compile time)

servlet-api在编译时需要在类路径中,它可能与服务器一起存在,但它在运行时可用(而不是在编译时)

回答by JB Nizet

The servlet API is not part of Java SE, but of Java EE. It's not bundled with the JDK. Find the javaee jar file in your Glassfish install and put it in your compilation classpath.

servlet API 不是 Java SE 的一部分,而是 Java EE 的一部分。它没有与 JDK 捆绑在一起。在 Glassfish 安装中找到 javaee jar 文件并将其放入编译类路径中。

回答by Suraj Chandran

Make sure you have servlet-api.jarin your classpath because is not part of the standard Java SDK.

确保您的类路径中有servlet-api.jar,因为它不是标准 Java SDK 的一部分。