Tomcat jar 是否要添加到类路径中?
由于像 servlet.jar 这样的 jar 文件通常不会单独下载,而是作为 tomcat/lib 文件夹的一部分,我是否应该在类路径中向它们添加一个条目?这是常见做法吗?
我用的是Ubuntu。
Since jars like servlet.jar are usually not downloaded on their own, but rather come part of tomcat/lib folder, should I just add an entry to them in the classpath? Is that the common practice?
I use Ubuntu.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您想要编译 servlet 类时,您只需要自己引用它们。如何做到这一点又取决于编译所使用的工具。
如果您使用普通的
javac
,那么您可以在%CLASSPATH%
中引用它们。但即便如此,这也被认为是一种糟糕的做法,因为这可能会污染所有其他 Java 编译/应用程序的默认类路径。而是编写一个 shell 文件,利用javac
命令的-cp
属性在当前执行环境上设置正确的类路径。如果您使用的是像 Eclipse/Netbeans 这样的不错的 IDE,那么您应该将服务器集成到 IDE 中并将项目与其关联。然后 IDE 将负责正确设置构建路径。那么你不需要设置任何环境变量。
当您想要运行它们时,不需要引用它们。 servlet 容器将自行处理它。
另请参阅:
You only need to reference them yourself when you want to compile servlet classes. How to do that depends in turn on the tools used for compilation.
If you're using plain
javac
, then you could reference them in%CLASSPATH%
. But even then, that's considered a poor practice since that would potentially pollute the default classpath of all other Java compilations/applications. Rather write a shell file which sets the classpath right on the current execution environment by utilizing the-cp
attribute ofjavac
command.If you're using a bit decent IDE like Eclipse/Netbeans, then you should just integrate the server in the IDE and associate the project with it. The IDE will then take care about setting the buildpath right. You don't need to set any environment variables then.
You do not need to reference them when you want to run them. The servletcontainer will take care about it by itself.
See also:
如果您在 Tomcat 上运行 Web 应用程序,则 servlet-api.jar 位于类路径中。
If you are running a web application on Tomcat then the servlet-api.jar is in the classpath.