如何从命令提示符编译servlet? [英] How to compile servlets from command prompt?

查看:148
本文介绍了如何从命令提示符编译servlet?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从命令提示符编译一个非常基本的 servlet ,但它总是不成功,编译器告诉我如下:

I'd like to compile a very basic servlet from command prompt, but it is always unsuccessful and the compiler tells me the following:

 error: package javax.servlet does not exist.

我搜索的解决方案,我发现我需要包括 servlet .jar 库到我的PATH。
我相信我。
我坚信这些库在我的计算机的位置是:

I googled for the solution and I found that I need to include the servlet.jar libraries into my PATH. I believe I did. I strongly believe that the location for those libraries in my computer is:

C:\apache-tomcat-7.0.23\lib\servlet-api.jar\ 

和结束部分)如下:

%JAVA_HOME%\bin;C:\apache-tomcat-7.0.23\lib\servlet-api.jar\

对我来说,它显然不是。任何人都可以告诉我可能是什么问题?

For me, it looks ok, but it is obviously not. Can anyone tell me what could be the problem?

推荐答案

classpath不是路径...你不需要它环境变量。
可以使用选项-cp或-classpath设置javac的类路径(还有其他几种方法)。
javac使用环境变量CLASSPATH来查找类,这可以是有用的,也可以是难以跟踪问题的源。

classpath not path ... and you don't need it as an evironment variable. You can set the classpath for javac with option -cp or -classpath (several other ways are also available). javac uses the environment variable CLASSPATH to look for classes, that can be useful and can also be a source for hard-to-track-down-problems.

编译使用库(即来自标准JavaSE外部的类)的java文件的示例如下:

An example to compile a java file that uses a library(that is classes from outside the standard JavaSE) would be:

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

如果您的环境变量CLASSPATH包含您需要的库,您可能需要:

if your environmental variable CLASSPATH contains libraries you need you might want to do:

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

(请注意,我没有访问Windows机器,因此没有测试上面语法的特殊部分)
(还要注意,在这个例子中C:​​\ apache-tomcat-7.0.23 \lib \servlet-api.jar是一个jar文件,而不是一个目录,它可能是从您的问题在您的机器上)
对于Windows操作系统上的命令行编译一个好主意是要正确设置环境变量JAVA_HOME,并在PATH中设置JDK的bin目录。

(please be aware that I don't have access to a windows machine, and therefore haven't tested the idiosyncratic parts of the syntax above) (also note that in this example "C:\apache-tomcat-7.0.23\lib\servlet-api.jar" is a jar file and not a directory which it might be from your question on your machine) For command line compiling on windows OS it is a good idea to have the environmental variable JAVA_HOME set correctly and the bin directory of the JDK in the PATH.

但我建议您编写compile-compile-execute-deploy通过/在IDE中进行servlet开发,然后通过命令行确定如何使用JDK。
Java Servlet不是独立的可执行类,但需要部署到例如要测试/使用的tomcat中。

I do however suggest getting to write-compile-execute-deploy via/in an IDE for servlet development before figuring out how to do it with just the JDK from a command line. Java Servlets are not stand-alone executable classes but needs to be deployed into for example tomcat to be tested/used.

这篇关于如何从命令提示符编译servlet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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