Note that there are some explanatory texts on larger screens.

plurals
  1. POHow 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?
    primarykey
    data
    text
    <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>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. COIf I were you, I would rephrase the question a bit to avoid having it closed as a duplicate of http://stackoverflow.com/questions/5444246
      singulars
    2. COThis is against the specifications and can lead to some complications if you don't pay attention to which classes are where, but there is nothing hindering you. **What was your question, again**?
      singulars
    3. CO@Fredrik - I know, I wrote it, but the other question was - "How to create a class loader that doesn't delegate to the parent", I got the answer, but realized that for what I need, I need to do a parent-last, e.g. one that *does* delegate to the parent, but only after searching the child, e.g. current. Just like WebLogic does, and any decent multiple application container. E.g. WebLogic uses Xerces version X but I like version Y, so WebLogic doing parent-last is again the specifications? Please provide a reference that confirms that, I'd like to ask IBM's tech support what they make of it.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload