Java 配置构建路径或 WEB-INF/lib 文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13679112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 13:34:42  来源:igfitidea点击:

Java Configuring Build Paths or WEB-INF/lib folder

javajakarta-eebuildjarclasspath

提问by user962206

I've seen a lot of tutorials and applications that put their jars inside a build path while others put it inside their web-inf/lib folder, Are there any significant difference? What are the pros and cons of both? What is an indicator for me to put a certain jar inside the libs folder and put the jar in the build path?

我已经看到很多教程和应用程序将他们的 jars 放在构建路径中,而其他人则将它放在他们的 web-inf/lib 文件夹中,有什么显着区别吗?两者的优缺点是什么?我将某个 jar 放入 libs 文件夹并将该 jar 放入构建路径的指标是什么?

回答by Tomer

An application with WEB-INF folder is a web application. Meaning it will be packed as a WAR file and deployed on a server.

带有 WEB-INF 文件夹的应用程序是一个 Web 应用程序。这意味着它将被打包为 WAR 文件并部署在服务器上。

If you are using an IDE like eclipse, and you export the project as a WAR file, it will automatically take jars under the lib folder and pack them with the WAR and that will make them available for the application running on the server (jars under WEB-INF/lib are included in the application classpath).

如果您使用的是 Eclipse 之类的 IDE,并且您将项目导出为 WAR 文件,它会自动将 lib 文件夹下的 jars 与 WAR 一起打包,这将使它们可用于服务器上运行的应用程序(jars 下WEB-INF/lib 包含在应用程序类路径中)。

If you just put them anywhere else and include them in the build path, when you export the project you have to declare that you want the jars in the build path to be included as well.

如果您只是将它们放在其他任何地方并将它们包含在构建路径中,那么在导出项目时,您必须声明您希望构建路径中的 jar 也包含在内。

Basically, there is no big difference but if you know you need this jar in runtime (i.e. after deploying the application to the server), it is better to put it under the WEB-INF/lib folder.

基本上,没有太大区别,但是如果您知道在运行时需要这个 jar(即在将应用程序部署到服务器之后),最好将它放在 WEB-INF/lib 文件夹下。

回答by Tomasz Nurkiewicz

What do you mean by build path? Most of the JARs you use need to be available during building (compiling) because your code depends on them - otherwise it won't compile.

构建路径是什么意思?您使用的大多数 JAR 都需要在构建(编译)期间可用,因为您的代码依赖于它们 - 否则将无法编译。

The real question is between placing JARs in WEB-INF/libvs. your container /libdirectory. In general you should always put your JARs inside an application in WEB-INF/lib. Placing them globally has several consequences:

真正的问题是将 JAR 放入WEB-INF/lib容器/lib目录与容器目录之间。通常,您应该始终将 JAR 放在WEB-INF/lib. 将它们全局放置有几个后果:

  • singletons are now global to all web applications (one class loader) if multiple deployed

  • it's easier to introduce memory leak if a library holds a reference to any of your classes (see above)

  • you don't duplicate the same classes (each web application will reuse the same class as opposed to having a separate copy in each class loader

  • 如果多个部署,单例现在对所有 Web 应用程序是全局的(一个类加载器)

  • 如果库包含对任何类的引用,则更容易引入内存泄漏(见上文)

  • 您不会复制相同的类(每个 Web 应用程序将重用相同的类,而不是在每个类加载器中都有一个单独的副本