I've got a question regarding the guarantees, if any, in the following scenario (note that the question is not "How to do this in a different way?", the question is really about class loading order in the following case (to better understand how class loading works).
Here's the hypothetical scenario... There's a .war file that has the following (partial) directory structure:
WEB-INF/classes/com/acme/Bunny.class
.
.
.
WEB-INF/lib/acme.jar
Both Bunny.class files have import referencing other classes from acme.jar
Bunny.class in WEB-INF/classes/... is the only class that has the same name/path that a class from acme.jar.
The .jar file acme.jar also contains com.acme.Bunny (and there are a no special class loader tricks used).
I understand that the Java spec guarantees that a class won't be loaded until it is actually used (or "manually class-loaded" on purpose) by the program, which is why if you stuff thousands of .jar, say, in a .war, the classloader(s) don't start classloading tens of thousands of classes.
(edit)
But what about the order in which the
two classes in the examples above are
loaded?
should have been phrased:
But how is it decided which one of the
two classes above is loaded?
or something like that :)
There's one guarantee made: com.acme.Bunny shall be used before any other class from com.acme....
Basically, on Wikipedia, the following is written:
The most complex JAR hell problems
arise in circumstances that take
advantage of the full complexity of
the classloading system. A Java
program is not required to use only a
single "flat" classloader, but instead
may be composed of several (or, in
fact, an indefinite number of) nested,
cooperating classloaders. Classes
loaded by different classloaders may
interact in complex ways not fully
comprehended by a developer, leading
to inexplicable errors or bugs.
So I'm wondering: can I be sure that /classes/com/acme/Bunny.class will be classloaded before the one from .jar inside the WEB-INF/lib/ dir or not?
See Question&Answers more detail:
os