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?


回答1:


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.




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/34728795/what-happens-if-we-put-two-different-versions-of-jar-files-in-classpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!