Java method works in 1.5 but not 1.6
Asked Answered
W

7

14

I have an application which has been running happily under Java 1.5 for around a year. We've just had the boxes updated and had Java 1.6 installed.

After deploying the app to the new server we've found the application is throwing an exception when it tries to transform some XML. We couldn't understand why this was happening until we deployed it locally and the same happened. After changing the SDK to v1.5 the problem stopped and the application runs fine.

Here's the method's source:

import java.io.StringWriter;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Element;
import org.w3c.dom.Node;


   public static String xmlToString(Node node) {
    try {
        Source source = new DOMSource(node);
        StringWriter stringWriter = new StringWriter();
        Result result = new StreamResult(stringWriter);
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.transform(source, result);
        return stringWriter.getBuffer().toString();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return null;
   }

It's crashing on the "transformer.transform(source, result);" line with exception:

Exception in thread "main" java.lang.AbstractMethodError: org.apache.xerces.dom.DocumentImpl.getXmlStandalone()Z
    at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.setDocumentInfo(DOM2TO.java:373)
    at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:127)
    at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:94)
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(TransformerImpl.java:662)
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:708)
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:313)

Does anyone know of any changes made to Java between the two versions which would cause this? What would be the easiest fix?

Thanks for your help.

Waterspout answered 10/8, 2009 at 15:5 Comment(2)
It seems you have a conflicting Xerxes implementation somewhere in your class path.Glaciology
What xml related jars are in your classpath?Freezedrying
T
18

I don't remember if it was between 1.4 and 1.5 or 1.5 and 1.6, but the Xalan libraries that shipped with the JVM from Sun changed their package name. I ran into something similar about 2 years ago. I think what I had to do was explicitly ship my own xalan implementation to fix the problem.

UPDATE: This might have been what I was thinking of, though it still could be related to your problem link text

Torpid answered 10/8, 2009 at 15:8 Comment(3)
You're supposed to use only the JAXP API or depend on the xerces libraries themselves, never on the sun-supplied implementation (which will vary between releases).Spun
I see a case where this is happening and the code is calling TransformerFactory to create a Transformer, then transform() on the Transformer and it is throwing this exception. This seems to be the right way to do it. The code was working fine in Java 5 and after upgrading to Java 6 it is throwing this exception. Including my own copy of xalan fixed the issue.Cultural
where do I find my own copy of xalan? and how do I include and replace whats given by oracle???Trifoliate
T
7

This problem is known to occur on JDK 1.6 with an older xerces.jar which, when on classpath, provides its own DocumentBuilderFactory.

The problem does not occur when using the platform default factory.

You may want to check your WEB-INF/lib or equivalent.

Turgeon answered 30/11, 2011 at 2:29 Comment(0)
T
4

It is the problem because of jar(Xalan) version conflict. Remove the jars and give a try

Toxoid answered 10/8, 2009 at 16:27 Comment(2)
I tried adding a new Xerces JAR but this did not solve as per the top upvoted answer, so I tried your suggestion and removed the Xerces JAR from my build path and it worked, thanks!Finagle
XALAN comes in rt.jar Are you saying I should remove jar from jre/lib path?Trifoliate
N
2

I encounter this same java.lang.AbstractMethodError in my code.

At the time changing the version of any libraries was not an option, but I found a workaround by comparing with other code that mysteriously worked. Perhaps this might helps others out there.

It all had to do with the Document I passed into DOMSource(). Originally I had created a document in the standard way:

    private static Document documentFromInputStream(InputStream in) throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(new InputSource(in));
    return doc;
}

To work around this issue, I change the factory line as follows:

        DocumentBuilderFactory factory = new DocumentBuilderFactoryImpl();

Now I no longer get the exception.

Nepean answered 30/10, 2013 at 20:21 Comment(1)
Great! dont have to deal with jar conflict. Thank you.Lowell
A
0

You may want to use latest version from Xerces (I believe it should be compitable with JDK1.6)

Absalom answered 11/4, 2011 at 13:50 Comment(0)
S
0

I had the same problem & replaced the xercesImpl-2.0.2.jar file with xercesImpl-2.11.0.jar in class path of my application. Its working fine.

Spotless answered 19/5, 2015 at 17:11 Comment(0)
T
0

This worked for me.

 TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
            transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    DOMSource source = new DOMSource(doc);
            StreamResult result = new StreamResult(sWout);
            transformer.transform(source, result);
Trifoliate answered 10/8, 2016 at 12:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.