title: Order of class loading from a .war file
body: <p>I've got a question regarding the guarantees, if any, in the following scenario (note that the question is not <em>"How to do this in a different way?"</em>, the question is really about class loading order in the following case (to better understand how class loading works).</p>
  
  <p>Here's the hypothetical scenario... There's a <em>.war</em> file that has the following (partial) directory structure:</p>
  
  <pre><code> WEB-INF/classes/com/acme/Bunny.class
   .
   .
   .
   WEB-INF/lib/acme.jar
  </code></pre>
  
  <p>Both <em>Bunny.class</em> files have import referencing other classes from <em>acme.jar</em></p>
  
  <p><em>Bunny.class</em> in <em>WEB-INF/classes/...</em> is the only class that has the same name/path that a class from <em>acme.jar</em>.</p>
  
  <p>The <em>.jar</em> file <em>acme.jar</em> also contains <em>com.acme.Bunny</em> (and there are a no special class loader tricks used).</p>
  
  <p>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.</p>
  
  <p><em>(edit)</em></p>
  
  <blockquote>
    <p>But what about the order in which the
    two classes in the examples above are
    loaded?</p>
  </blockquote>
  
  <p>should have been phrased:</p>
  
  <blockquote>
    <p>But how is it decided which one of the
    two classes above is loaded?</p>
  </blockquote>
  
  <p>or something like that :)</p>
  
  <p>There's one guarantee made: <em>com.acme.Bunny</em> shall be used before any other class from <em>com.acme...</em>.</p>
  
  <p>Basically, on Wikipedia, the following is written:</p>
  
  <blockquote>
    <p>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.</p>
  </blockquote>
  
  <p>So I'm wondering: can I be sure that <em>/classes/com/acme/Bunny.class</em> will be classloaded before the one from <em>.jar</em> inside the <em>WEB-INF/lib/</em> dir or not?</p>