what happens if we put two different versions of jar files in classpath? [closed]

↘锁芯ラ 提交于 2019-12-20 19:43:54


If i put two different versions of jar files in class path, what willl happen?
for example: log4j1.4.jar and log4j1.5.jar kept in classpath
what will happen?


While I also recommend not to do this, I still like to try to answer your original question:

Java has a classloader hierarchy, so if you have both JARs in different levels of the hierarchy, the classloader defines its precendence. Most popular example is the web application classloader hierarchy (Tomcat for example), where application classes have a higher priority than the comtainer classes (if both are applicable).

If you have both JARs in the same classloader (same level), the filesystem determines the order, which is unreliable from the developer's point-of-view, so consider it to be random. Only one loads, but you don't know which, and will maybe not even get errors from dependency problems. If you get dependency problems, they may be java.lang.Errors, such as VerifyError, NoClassDefFoundError, NoSuchMethodError.


One of two things might happen - you can get version 1.4, or you can get 1.5.

This is not recommended, might cause problems especially if you are using two versions which are far apart with slightly different API's, might cause compilation issues


Short answer is you don't want to do this.

Depending on how you use the classes provided by the JAR, you can have either a clear exception or weird behaviors.