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,

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

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
