简体   繁体   English

在lib文件夹中添加jar文件和在classpath中进行设置之间的区别

[英]diffrence between adding jars files in lib folder and setting it in classpath

could you please explain the difference in addng jars in lib folder and adding jars to classpath . 您能解释一下lib文件夹中的addng jar和将jar添加到classpath的区别吗?

are both just a way to add jars(anyone will work) or is there any difference ? 都是添加jar的一种方法(任何人都可以使用)还是有什么区别? I tried searching but couldn't find any answer. 我尝试搜索,但找不到任何答案。

If you have a web application project that bundles into a WAR, then adding the jar files into WEB-INF/lib will automatically put all these jar in the classpath of the application when being deployed in a servlet container (Jetty, Tomcat) or in an application server (GlassFish, Wildfly). 如果您有一个捆绑到WAR中的Web应用程序项目,则将JAR文件添加到WEB-INF / lib中后,当将它们部署到servlet容器(Jetty,Tomcat)或Windows XP中时,会将所有这些jar自动放入应用程序的类路径中。应用程序服务器(GlassFish,Wildfly)。 Note that these libraries will only be available for your single application being deployed, not for any other app deployed in this server. 请注意,这些库仅适用于正在部署的单个应用程序,不适用于此服务器中部署的任何其他应用程序。

If you have a simple jar with a lib folder inside it, then adding any jar into this folder will do nothing . 如果您有一个带有jar文件夹的简单jar,那么将任何jar添加到该文件夹​​中将无济于事 For jar files that must be executed on their own, you need to specify the libraries to use in the classpath, otherwise your jar will not run. 对于必须自己执行的jar文件,您需要指定要在类路径中使用的库,否则jar将无法运行。 For this case, it will be better to have the libraries inside a lib filder outside the jar, so you could reference them in the MANIFEST file. 对于这种情况,最好将这些库放在jar之外的lib filder中,因此可以在MANIFEST文件中引用它们。 On the other hand, you may use maven to generate a fat jar . 另一方面,您可以使用maven生成一个胖子罐

If you are using any third party jar files then you need to add it in classpath because you java application tries to find out the class which you are using in you app. 如果您正在使用任何第三方jar文件,则需要将其添加到classpath中,因为Java应用程序会尝试找出您在应用程序中使用的类。 Adding jars in classpath means you are explicitly adding the jar files in classpath at the time of execution. 在类路径中添加jar意味着您在执行时在类路径中显式添加了jar文件。 Adding jars in lib folder will be done in eclipse will internally add the jar files in classpath at the time of execution. 在lib文件夹中添加jar将在eclipse中完成,在执行时将在内部将jar文件添加到classpath中。

To verify both the things execute the following command 要验证这两种情况,请执行以下命令

ps -eaf | ps -eaf | grep -i java grep -i Java

which will display your app with the dependencies(jar files) in classpath. 它将在类路径中显示具有依赖项(jar文件)的应用。

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

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