【问题标题】:Apache Xerces throws java.lang.NoSuchMethodError when using XSAttributeUse.getValueConstraintValue()Apache Xerces 在使用 XSAttributeUse.getValueConstraintValue() 时抛出 java.lang.NoSuchMethodError
【发布时间】:2017-03-16 11:33:42
【问题描述】:

我的任务是提取给定复杂类型中每个可选属性的(名称,默认值)对。 我有以下代码:

XSObjectList attrList = typeDefinition.getAttributeUses();
for (int i = 0; i < attrList.getLength(); i++) {
    XSAttributeUse attributeUse = (XSAttributeUse) attrList.item(i);

    if (!attributeUse.getRequired()) {
        String name = attributeUse.getValueConstraintValue().getNormalizedValue();
        String value = /* extracting the default value */;
    }
}

这会产生错误:

Exception in thread "main" java.lang.NoSuchMethodError: org/apache/xerces/xs/XSAttributeUse.getValueConstraintValue()Lorg/apache/xerces/xs/XSValue; (loaded from path\to\sdk\jre\lib\xml.jar by <Bootstrap Loader>) called from class my.package.xsd.XSDParser (loaded from file:/path/to/the/project/SwidSigner/target/classes/ by sun.misc.Launcher$AppClassLoader@29b444c2).
    at my.package.xsd.XSDParser.getTypeAttributes(XSDParser.java:54)
    at my.package.xsd.XSDParser.getAttributes(XSDParser.java:37)
    at my.package.Main.main(Main.java:29)

更重要的是,当我将其更改为:

XSObjectList attrList = typeDefinition.getAttributeUses();
for (int i = 0; i < attrList.getLength(); i++) {
    XSAttributeUse attributeUse = (XSAttributeUse) attrList.item(i);
    XSAttributeDeclaration attributeDecl = attributeUse.getAttrDeclaration();

    if (!attributeUse.getRequired()) {
        String name = attributeDecl.getValueConstraintValue().getNormalizedValue();
        String value = /* extracting the default value */;
    }
}

错误仍然发生,但它抱怨 XSAttributeDeclaration 类。使用其他方法没有报错,如

XSComplexTypeDefinition.getAttributeUses()

我正在使用 Maven 导入 Xerces:

<dependency>
    <groupId>xerces</groupId>
    <artifactId>xercesImpl</artifactId>
    <version>2.11.0</version>
</dependency>

【问题讨论】:

  • 您使用的是什么版本的 Java?看起来它试图从 JRE 的 lib 目录中加载 Xerces 类。
  • 我使用的是 Java 8。如何让它从 Maven 加载 Xerces 类?

标签: java xml xsd xerces nosuchmethoderror


【解决方案1】:

你有一个带有 Xerces 类的流氓 jar。

检查您安装的 JRE 的 lib 目录。它似乎正在从一个名为xml.jar 的 jar 中加载有问题的类。我不确定这个 jar 是从哪里来的,因为我在我目前正在使用的任何 Java 安装中都没有看到它(Linux 和 OSX 上的 Java 8)。

如果您删除(或移动)此 jar,问题应该会得到解决。请注意,这可能会对您系统上的其他 Java 程序产生意想不到的后果。

【讨论】:

    【解决方案2】:

    NoSuchMethodError 表示编译时类路径中存在的代码在运行时类路径中不存在。检查是否有任何差异。如果您使用的是为您生成“导入”的 IDE,请检查它是否正在导入您希望使用的包。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-06
      • 2021-03-15
      • 1970-01-01
      • 2015-12-10
      • 1970-01-01
      • 2013-07-24
      • 1970-01-01
      • 2017-01-29
      相关资源
      最近更新 更多