Java方法在1.5中有效,但在1.6中无效

发布于 2024-07-30 14:17:31 字数 2155 浏览 16 评论 0原文

我有一个应用程序,它已经在 J​​ava 1.5 下愉快地运行了大约一年。 我们刚刚更新了盒子并安装了 Java 1.6。

将应用程序部署到新服务器后,我们发现应用程序在尝试转换某些 XML 时抛出异常。 我们无法理解为什么会发生这种情况,直到我们在本地部署它并发生了同样的情况。 将 SDK 更改为 v1.5 后,问题消失,应用程序运行正常。

这是该方法的源代码:

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

它在“transformer.transform(source, result);”上崩溃 异常行:

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)

有谁知道两个版本之间对 Java 所做的任何更改会导致这种情况吗? 最简单的修复是什么?

感谢您的帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

半世蒼涼 2024-08-06 14:17:31

我不记得它是在 1.4 和 1.5 之间还是 1.5 和 1.6 之间,但是 Sun 的 JVM 附带的 Xalan 库更改了它们的包名称。 大约两年前我遇到过类似的事情。 我认为我必须做的是明确地发布我自己的 xalan 实现来解决问题。

更新:这可能是我想到的,尽管它仍然可能与您的问题有关 链接文本

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

你另情深 2024-08-06 14:17:31

已知此问题会发生在带有旧版 xerces.jar 的 JDK 1.6 上,该旧版 xerces.jar 在类路径上时会提供自己的 DocumentBuilderFactory。

使用平台默认工厂时不会出现该问题。

您可能需要检查 WEB-INF/lib 或同等文件。

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.

始终不够 2024-08-06 14:17:31

这是因为 jar(Xalan) 版本冲突的问题。 取出罐子并尝试一下

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

双手揣兜 2024-08-06 14:17:31

我在代码中遇到了同样的 java.lang.AbstractMethodError

当时,更改任何库的版本都不是一个选择,但我通过与其他神秘工作的代码进行比较找到了解决方法。 也许这可能会对其他人有所帮助。

这一切都与我传递给 DOMSource() 的文档有关。 最初我以标准方式创建了一个文档:

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

为了解决此问题,我按如下方式更改了工厂行:

        DocumentBuilderFactory factory = new DocumentBuilderFactoryImpl();

现在我不再遇到异常。

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.

ι不睡觉的鱼゛ 2024-08-06 14:17:31

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

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

夜未央樱花落 2024-08-06 14:17:31

我有同样的问题& 在我的应用程序的类路径中将 xercesImpl-2.0.2.jar 文件替换为 xercesImpl-2.11.0.jar 。 它工作正常。

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.

枫以 2024-08-06 14:17:31

这对我有用。

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文