Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 496
Naresh Avatar asked Jan 11 '16 18:01

Naresh


People also ask

Does order of classpath JARs matter?

It is specified by the order in which the resources (i.e. usually jar files) are specified using -classpath option. Resources 'earlier' on the classpath take precedence over resources that are specified after them.

How do I specify multiple JARs in classpath?

In general, to include all of the JARs in a given directory, you can use the wildcard * (not *. jar ). The wildcard only matches JARs, not class files; to get all classes in a directory, just end the classpath entry at the directory name.


1 Answers

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.

like image 124
Thomas Jacob Avatar answered Oct 20 '22 23:10

Thomas Jacob