15

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?

Naresh
  • 333
  • 1
  • 2
  • 9

3 Answers3

24

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.

Thomas Jacob
  • 618
  • 6
  • 12
  • 4
    Its a good answer, except that it gives to much credit to the jars which are just storage vessels - its about the classes and packages inside the jars. There is only a conflict if both jars define the exact same fully qualified class names. The JVM will most definitely load both jars - but it will not load classes from them that are already loaded. – Gimby Jan 11 '16 at 19:58
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

Nir Levy
  • 12,750
  • 3
  • 21
  • 38
1

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.

Gaël J
  • 11,274
  • 4
  • 17
  • 32