title: How do I create a parent-last / child-first ClassLoader in Java, or How to override an old Xerces version that was already loaded in the parent CL?
body: <p>I would like to create a parent-last / child-first class loader, e.g. a class loader that will look for classes in the child class loder first, and only then delegate to it's parent ClassLoader to search for classes.</p>
  
  <p><strong>Clarification:</strong></p>
  
  <p>I know now that to get complete ClassLoading seperation I need to use something like a URLClassLoader passing null as it's parent, thanks to <a href="https://stackoverflow.com/questions/5444246/how-do-i-create-a-classloader-that-will-not-search-the-parent-for-loading-classes/5444309#5444309">this answer</a> to my previous question</p>
  
  <p>However the current question comes to help me resolve this issue: </p>
  
  <ol>
  <li><p>My code + dependent jars are being loaded into an existing system, using a ClassLoader that sets that System's ClassLoader as it's parent (URLClassLoader)</p></li>
  <li><p>That System uses some libraries of a version not compatible with the one I need (e.g. older version of Xerces, that doesn't allow me to run my code)</p></li>
  <li><p>My code runs perfectly fine if runs stand alone, but it fails if runs from that ClassLoader</p></li>
  <li><p>Howerver I do need access to many other classes within the parent ClassLoader</p></li>
  <li><p>Therefore I want to allow me to Override, the parent classloader "jars" with my own: If a class I call is found in the child class loader (e.g. I provided a newer version of Xerces with my own jars, instead of the one users by the ClassLoader that loaded my code and jars.</p></li>
  </ol>
  
  <p>Here is the System's code that loads my code + Jars (I can't change this one)</p>
  
  <pre><code>File addOnFolder = new File("/addOns"); 
  URL url = addOnFolder.toURL();         
  URL[] urls = new URL[]{url};
  ClassLoader parent = getClass().getClassLoader();
  cl = URLClassLoader.newInstance(urls, parent);
  </code></pre>
  
  <p>Here is "my" code (taken fully from the Flying Sauser "Hello World" code demo):</p>
  
  <pre><code>package flyingsaucerpdf;
  
  import java.io.*;
  import com.lowagie.text.DocumentException;
  import org.xhtmlrenderer.pdf.ITextRenderer;
  
  public class FirstDoc {
  
      public static void main(String[] args) 
              throws IOException, DocumentException {
  
          String f = new File("sample.xhtml").getAbsolutePath();
          System.out.println(f);
          //if(true) return;
          String inputFile = "sample.html";
          String url = new File(inputFile).toURI().toURL().toString();
          String outputFile = "firstdoc.pdf";
          OutputStream os = new FileOutputStream(outputFile);
  
          ITextRenderer renderer = new ITextRenderer();
          renderer.setDocument(url);
          renderer.layout();
          renderer.createPDF(os);
  
          os.close();
      }
  }
  </code></pre>
  
  <p>This works standalone (running main) but fails with this error when loaded through the parent CL:</p>
  
  <blockquote>
    <p><strong>org.w3c.dom.DOMException: NAMESPACE_ERR:</strong> An attempt is made to
    create or change an object in a way
    which is incorrect with regard to
    namespaces.</p>
  </blockquote>
  
  <p>probably because the parent system uses Xerces of an older version, and even though I provide the right Xerces jar in the /addOns folder, since it's classes were already loaded and used by the parent System, it doesn't allow my own code to use my own jar due to the direction of the delegation. I hope this makes my question clearer, and I'm sure it has been asked 
  before. (Perhaps I don't ask the right question)</p>