问题描述
我在编译java servlet时遇到大问题。据我所见,香港专业教育学院做了我需要做的一切,我已经正确安装了tomcat 7,并且tomcat正在工作。据我所知,我需要将servlet.jar包添加到我的类路径中。在我的系统上似乎没有servlet.jar,但是我从tomcat docs可以理解,现在它是servlet-api.jar。
Im having big problems compiling java servlets. As far as i can see, ive done everything i need to do, ive installed tomcat 7 correctly, and tomcat is working. As i understand, i need to add servlet.jar package to my classpath. There doesnt seem to be a servlet.jar on my system but from what i can understand from the tomcat docs, its now servlet-api.jar.
我做过这个,通过编辑 / etc / environment
中的类路径:
Ive done this, by editing the classpath at /etc/environment
:
PATH =/ usr / local / sbin:/ usr / local / bin:/ usr / sbin:/ usr / bin:/ sbin:/ bin:/ usr / games
CLASSPATH =/ usr / share / tomcat7 / lib / servlet-api.jar
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"CLASSPATH="/usr/share/tomcat7/lib/servlet-api.jar"
不幸的是,仍然没有运气,我无法编译java servlet,我仍然会收到有关 javax.servlets缺少符号的警告
。
Unfortunately, still no luck, i cant compile java servlets, and im still get warnings about missing symbols for javax.servlets
.
我正在使用ubuntu 11.10 x64。有什么想法吗?
Im using ubuntu 11.10 x64. Any ideas?
推荐答案
PATH =/ usr / local / sbin:/ usr / local / bin:/ usr / sbin:/ usr / bin:/ sbin:/ bin:/ usr / gamesCLASSPATH =/ usr / share / tomcat7 / lib / servlet-api.jar
看起来不错。但是, PATH
与特定问题无关。 Java没有以任何方式使用它。它仅供操作系统平台用于查找可执行文件。 CLASSPATH
类似,但仅供Java用于查找在编译和/或运行时使用的类。
That looks fine. The PATH
is irrelevant to the particular problem though. It's not been used by Java in any way. It's only used by the operating system platform to look for executabeles. The CLASSPATH
is similar, but only used by Java to look for classes which are to be used during compile and/or runtime.
您的问题很可能是因为您使用 -cp
或 -classpath
<$ c $参数c> javac 命令。在这种情况下, CLASSPATH
环境变量将被忽略(这也适用于 java
顺便说一句,当使用 -jar
参数时。)
Your problem is most likely caused because you used -cp
or -classpath
argument of javac
command. In that case, the CLASSPATH
environment variable will be ignored (this also applies to java
command by the way, also when -jar
argument is used).
你需要通过 CLASSPATH
环境变量或 -cp
或 -classpath
参数。一般的建议是忘记 CLASSPATH
环境变量,因为当你想要做的不仅仅是Hello World时,这被认为是一种糟糕的做法。您可以使用在
。 -cp
或 -classpath
参数中指定多个路径:
You need to specify the classpath by only either the CLASSPATH
environment variable or the -cp
or -classpath
argument. The general recommendation is to forget about the CLASSPATH
environment variable at all as that's considered a poor practice whenever you want to do a bit more than just "Hello World". You can specify multiple paths in the -cp
or -classpath
argument using :
.
$ cd /path/to/package/root/of/your/servlet/code
$ javac -cp .:/path/to/servlet-api.jar com/example/YourServlet.java
如果你厌倦了每次重复输入,只需将它放在 .sh
脚本中,或者使用像Ant这样的构建工具它允许通过XML文件进行配置,或者只使用Eclipse,Netbeans或IntelliJ等IDE,只需保存源文件即可自动完成。
If you get tired of repeating typing this everytime, just put it in a .sh
script, or use a build tool such as Ant which allows configuration by a XML file, or just use an IDE like Eclipse, Netbeans or IntelliJ which will do it all automagically when you just save the source file.
这篇关于Java servlet不会编译 - 找不到javax.servlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!