简体   繁体   中英

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 .

are both just a way to add jars(anyone will work) or is there any difference ? 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). 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 . 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. 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. On the other hand, you may use maven to generate a fat jar .

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. Adding jars in classpath means you are explicitly adding the jar files in classpath at the time of execution. Adding jars in lib folder will be done in eclipse will internally add the jar files in classpath at the time of execution.

To verify both the things execute the following command

ps -eaf | grep -i java

which will display your app with the dependencies(jar files) in classpath.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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