title: How to prevent xalan.jar that has META-INF\services\javax.xml.transform.TransformerFactory from taking over JDK 1.6 built in Xalan implementation?
body: <p>Consider this code (based entirely on flying saucer's "getting started" code, their rights reserved):</p>
  
  <pre><code>package flyingsaucerpdf;
  import java.io.File;
  import java.io.FileOutputStream;
  import java.io.OutputStream;
  
  import org.xhtmlrenderer.pdf.ITextRenderer;
  
  
      public class PDFMaker {
          public static void main(String[] args) throws Exception {
              new PDFMaker().go();
          }
  
          public void go() throws Exception {
              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>Few facts:</p>
  
  <ol>
  <li>Running it standalone (calling main) with JDK 1.6 or 1.5 works perfectly (PDF is generated)</li>
  <li>But when loaded via a URLClassLoader from an existing web application it fails with this error:</li>
  </ol>
  
  <pre>
  
  Caused by: org.w3c.dom.DOMException: NAMESPACE_ERR: An attempt is made to create or change an object in a way which is incorrect with regard to namespaces.
      at org.apache.xerces.dom.AttrNSImpl.setName(Unknown Source)
      at org.apache.xerces.dom.AttrNSImpl.(Unknown Source)
      at org.apache.xerces.dom.CoreDocumentImpl.createAttributeNS(Unknown Source)
      at org.apache.xerces.dom.ElementImpl.setAttributeNS(Unknown Source)
      at org.apache.xml.utils.DOMBuilder.startElement(DOMBuilder.java:307)
      ... 19 more
  </pre>
  
  <p>After looking for a while in the wrong place (for example, I created a child-first / parent-last class loader suspecting xalan / xerces jars, but it still fails), I finally narrowed down the root cause:</p>
  
  <p>It seems that the web application that loads my code, has an old <strong>xalan.jar</strong>, specification version 1.2 </p>
  
  <p>I did a little test, I ran the code above as standalone (which worked fine before) but this time I added the <strong>xalan.jar</strong> from the web app to it's classpath, and bingo, the same error as in the web app scenario</p>
  
  <p>So I inspected that old xalan.jar and wondered, what can cause the JVM to load it's old xalan implementation instead of the JDK's? after all my child-first class loader is also parent-last e.g. system in the middle, to say: searching the system classloader before the parent (to avoid loading parent overriden JDK jars, just like this case of the parent's xalan.jar overriding the JDK's xalan implementation)</p>
  
  <p>Then something cought my eyes - a file in: <strong>xalan.jar/META-INF/services/</strong> named <strong>javax.xml.transform.TransformerFactory</strong> with this content: </p>
  
  <pre><code>org.apache.xalan.processor.TransformerFactoryImpl
  </code></pre>
  
  <p>So I imediately pressed <kbd>Ctrl</kbd>+<kbd>T</kbd> in eclipse and looked for the full qualified name... only in <strong>xalan.jar</strong>!</p>
  
  <p>Then I searched only for "TransformerFactoryImpl", and this is what the JDK has:</p>
  
  <pre><code>com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl
  </code></pre>
  
  <p>Easy to see the difference</p>
  
  <p>So, if you read till here, my bottom line question is: <strong>How do I make my TransformerFactory use the JDK's implementation and not the old Xalan's one?</strong> (I can't remove that jar from the web app my code will be loaded from) </p>