2

How class loader prioritize the jars when we load jar using -classpath and adding jars in maven dependency ?

How does maven load jars? Does it follow any order based on order of dependency added in pom.xml ? Because few jar are behaving differently based on order (top or bottom) of adding jar.

My main concern is if I added jar though -classpath or top in maven dependency or bottom in maven dependency its behaving differently.

tryingToLearn
  • 10,691
  • 12
  • 80
  • 114
R D
  • 1,330
  • 13
  • 30

1 Answers1

1

Order in Classpath:

It is specified by the order in which the jars are specified. Jars 'earlier' on the classpath will be given priority over the jars that are specified `later'. Suppose you specify 2 jars in classpath :

 -cp jar1 jar2

Now both jars might have same package/class com.abc.Util.java. So at runtime the class lying in jar1 will be picked as jar1 is specified before jar2 in classpath.

More info in this SO post

Order in maven:

The same behavior as above can be achieved in maven by specifying higher priority jar in pom.xml above lower priority jar. I have tested this on maven 3.x and if you see the dependencies tab,

enter image description here

you can see that order will change if you change order in pom.xml

enter image description here

Summary: In both -classpath option as well as maven, the jar specified first wins. So your observation is correct.

ORDER in case of combination of -cp and maven: If you are using a combination of the two, then the -cp option will win the first priority.

One more thing regarding maven. Since, when you specify dependencies in pom.xml, it will automatically bring it's own dependencies (transitive dependencies).

So let's say you have specified dependency A in pom which brings dependency X version 1.1. After that you define dependency B in pom, which also needs dependency X but version 1.2. Now which version of X do you think will come?

The version that will come will depend on which dependency you have specified first. If jarA is specified first, X1.1 will come, if jarB specified first, X1.2 will come.

You can check this behavior in dependency-hierarchy tab, you will see some of the transitive dependencies will be shown omitted. E.g: below as some other jar would have brought commons-io 2.6 before

enter image description here

tryingToLearn
  • 10,691
  • 12
  • 80
  • 114
  • I think adding through "java -cp" will take first priority compare with adding in pom, is it correct ? – R D Aug 10 '18 at 06:23
  • If you are combining the two, then yes. thanks for asking this, i updated my answer – tryingToLearn Aug 10 '18 at 06:23
  • Which one is good practice, adding it in top of pom or through "java -cp" while combining both. – R D Aug 10 '18 at 06:26
  • defining in pom should be the first priority because when you share the pom, the other developers can also get the same behavior, otherwise you will need to specifically tell everyone to remember to put in the jar through `-cp`. With pom, you just specify the order once and you are done. – tryingToLearn Aug 10 '18 at 06:28
  • I am working on the java migration part, still not experimented this. Once done I will check and accept your answer!! – R D Aug 20 '18 at 06:27