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

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

问题描述

当我编译一个Java文件时,我得到了这个错误信息:

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

error: package javax.servlet does not exist

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

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作为Servlet容器位于 / Library / Tomcat /

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中的依赖。 / p>

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 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 的classpath参数将这些jars添加到类路径。

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.23\lib\servlet-api.jar MyTestServlet.java

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

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