简体   繁体   English

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

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

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: 我为Jave 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容器和使用命令行的简单文本编辑器。

You need to include the servlet-api JAR 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项目中包含这些: 如何在Eclipse项目中导入javax.servlet API?

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. 如果使用命令行来构建项目,则很可能需要将这些添加到javac的classpath参数中,以将这些jar添加到类路径中。

See this question: How to compile servlets from command prompt? 看到这个问题: 如何从命令提示符编译servlet?

The key part is: 关键部分是:

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

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. jar名称前面的路径可能因安装而异。 Just go to your lib folder of tomcat and copy the whole directory. 只需转到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! 希望这可以帮助!

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

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. javax.servlet包不是引导程序包或扩展程序包的一部分,因此必须手动将其添加到类路径中。 ALJI has shown you how to do this from the command line. ALJI已经向您展示了如何从命令行执行此操作。 The Wikipedia link above also provides examples. 上面的Wikipedia链接也提供了示例。

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类路径有一个基本的了解。

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

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