如何在命令行中为 Tomcat 编译 servlet?错误:包 javax.servlet 不存在 [英] How to compile a servlet for Tomcat in command line? error: package javax.servlet does not exist

查看:13
本文介绍了如何在命令行中为 Tomcat 编译 servlet?错误:包 javax.servlet 不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

error: package javax.servlet does not exist

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

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

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?

我使用 Tomcat 7 作为位于 /Library/Tomcat/ 的 Servlet 容器和带有命令行的简单文本编辑器.

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

推荐答案

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

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

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

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

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

这将在编译时包含依赖项,并在运行时使用 Tomcat 一个.

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

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

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

这个问题有一些关于在 Eclipse 项目中包含这些的有用信息:如何导入 javax.servlet我的 Eclipse 项目中的 API?

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?

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

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

请参阅此问题:如何从命令提示符编译 servlet?

关键部分是:

javac -classpath C:apache-tomcat-7.0.23libservlet-api.jar MyTestServlet.java

这篇关于如何在命令行中为 Tomcat 编译 servlet?错误:包 javax.servlet 不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆