简体   繁体   English

Java方法适用于1.5但不适用1.6

[英]Java method works in 1.5 but not 1.6

I have an application which has been running happily under Java 1.5 for around a year. 我有一个应用程序,它在Java 1.5下运行了大约一年。 We've just had the boxes updated and had Java 1.6 installed. 我们刚刚更新了盒子并安装了Java 1.6。

After deploying the app to the new server we've found the application is throwing an exception when it tries to transform some XML. 在将应用程序部署到新服务器之后,我们发现应用程序在尝试转换某些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. 将SDK更改为v1.5后问题停止,应用程序运行正常。

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);" 它正在崩溃“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? 有谁知道这两个版本之间对Java做出的任何改变会导致这种情况吗? What would be the easiest fix? 什么是最容易解决的问题?

Thanks for your help. 谢谢你的帮助。

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. 我不记得它是介于1.4和1.5还是1.5和1.6之间,但是Sun附带的JVM附带的Xalan库改变了它们的包名。 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. 我认为我必须做的是明确地发布我自己的xalan实现来解决问题。

UPDATE: This might have been what I was thinking of, though it still could be related to your problem link text 更新:这可能是我想到的,虽然它仍然可能与您的问题链接文本有关

This problem is known to occur on JDK 1.6 with an older xerces.jar which, when on classpath, provides its own DocumentBuilderFactory. 已知这个问题发生在具有旧xerces.jar的JDK 1.6上,当在classpath上时,它提供了自己的DocumentBuilderFactory。

The problem does not occur when using the platform default factory. 使用平台默认工厂时不会发生此问题。

You may want to check your WEB-INF/lib or equivalent. 您可能需要检查WEB-INF / lib或同等产品。

It is the problem because of jar(Xalan) version conflict. 这是因为jar(Xalan)版本冲突的问题。 Remove the jars and give a try 取出罐子试一试

I encounter this same java.lang.AbstractMethodError in my code. 我在代码中遇到了同样的java.lang.AbstractMethodError

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(). 这一切都与我传入DOMSource()的Document有关。 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. 现在我不再得到例外。

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. 我遇到了同样的问题,并在我的应用程序的类路径中用xercesImpl-2.11.0.jar替换了xercesImpl-2.0.2.jar文件。 Its working fine. 它的工作正常。

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);

您可能想要使用Xerces的最新版本(我相信它应该与JDK1.6兼容)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM