java 如何在命令行中为 Tomcat 编译 servlet?错误:包 javax.servlet 不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14104937/
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 15:05:29  来源:igfitidea点击:

How to compile a servlet for Tomcat in command line? error: package javax.servlet does not exist

javatomcatservletscompiler-errorsosx-mountain-lion

提问by cowls

I've got this error message when I compiled a Java file :

我在编译 Java 文件时收到此错误消息:

error: package javax.servlet does not exist

I installed a big .SH file for Jave EE SDK, a Java version gives me this:

我为 Java EE SDK 安装了一个大的 .SH 文件,Java 版本给了我这个:

java version "1.7.0_10"
Java(TM) SE Runtime Environment (build 1.7.0_10-b18)
Java HotSpot(TM) 64-Bit Server VM (build 23.6-b04, mixed mode)

Do I need to install something else?

我需要安装其他东西吗?

I am using Tomcat 7 as a Servlet Container located in /Library/Tomcat/and simple text editor with the command line.

我将 Tomcat 7 用作位于/Library/Tomcat/带有命令行的简单文本编辑器中的 Servlet 容器。

回答by cowls

You need to include the servlet-apiJAR in the compile time classpath.

您需要servlet-api在编译时类路径中包含JAR。

If you are using maven add this as a dependency in the pom.xml.

如果您使用 maven,请将其添加为 pom.xml 中的依赖项。

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

That wil include the dependency at compile time and use the Tomcat one at runtime.

这将包括编译时的依赖项,并在运行时使用 Tomcat 依赖项。

If not you should add Tomcat as project target runtime through Eclipse.

如果不是,您应该通过 Eclipse 添加 Tomcat 作为项目目标运行时。

This questions has some useful info on including these in an Eclipse project: How do I import the javax.servlet API in my Eclipse project?

这个问题有一些关于在 Eclipse 项目中包含这些的有用信息: How do I import the javax.servlet API in my Eclipse project?

If you are using command line to build the project, you will most likely need to add these to the classpath argument to javacto add these jars to the classpath.

如果您使用命令行来构建项目,您很可能需要将这些添加到 classpath 参数中javac以将这些 jars 添加到类路径中。

See this question: How to compile servlets from command prompt?

看到这个问题:How to compile servlets from command prompt?

The key part is:

关键部分是:

javac -classpath C:\apache-tomcat-7.0.23\lib\servlet-api.jar MyTestServlet.java

回答by praveen

A Windows User:

Windows 用户:

I faced this problem myself and here is the solution that worked fine

我自己也遇到过这个问题,这里是运行良好的解决方案

Just Add this path to your CLASSPATH environmental variable "C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\servlet-api.jar"

只需将此路径添加到您的 CLASSPATH 环境变量“C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\servlet-api.jar”

The path before the jar name can differ based on your installation. Just go to your lib folder of tomcat and copy the whole directory.

jar 名称之前的路径可能因您的安装而异。只需转到tomcat的lib文件夹并复制整个目录即可。

More info for beginners: You can find Environmental variables here MyComputer -> Properties ->Advanced Settings -> Advanced tab

初学者的更多信息:您可以在此处找到环境变量 MyComputer -> 属性 -> 高级设置 -> 高级选项卡

Now you can simply go to cmd prompt and type "javac Myclass.java"

现在您可以简单地转到 cmd 提示符并键入“javac Myclass.java”

Hope this helps!

希望这可以帮助!

回答by praveen

javac -classpath /Library/Tomcat/lib/servlet-api.jar *.java

回答by Matthew Petty

Read about java class paths here on Wikipedia.

在 Wikipedia 上阅读有关 java 类路径的信息

Ready closely the last paragraph under "Overview and architecture".

仔细准备“概述和架构”下的最后一段。

In your example

在你的例子中

The javax.servlet package is not part of the bootstrapped or extension packages, so it must be added manually to your classpath. ALJI has shown you how to do this from the command line. The Wikipedia link above also provides examples.

javax.servlet 包不是引导包或扩展包的一部分,因此必须手动将其添加到类路径中。ALJI 已向您展示了如何从命令行执行此操作。上面的维基百科链接也提供了示例。

Recommendation

推荐

Everyone hits these types issues when starting a new language. Google is full of tutorials that will help you gain a basic understanding of Java class paths.

每个人在开始一门新语言时都会遇到这些类型的问题。Google 提供了大量教程,可帮助您基本了解 Java 类路径。