zenn1337
zenn1337

Reputation: 491

Apache Xerces throws java.lang.NoSuchMethodError when using XSAttributeUse.getValueConstraintValue()

My task is to pull out a (name, default value) pair of every optional attribute in a given complex type. I have the following piece of code:

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

This produces the error:

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)

What's more, when I change it to:

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

The error still occurs, but it complains about XSAttributeDeclaration class instead. There is no error when using other methods, such as

XSComplexTypeDefinition.getAttributeUses()

I'm using Maven to import Xerces:

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

Upvotes: 0

Views: 1258

Answers (2)

Michael Kay
Michael Kay

Reputation: 163458

NoSuchMethodError means that there's code that was present in your compile-time classpath that isn't present in your run-time classpath. Check for any differences. If you're using an IDE that generates the "import"s for you, check that it's importing the packages you expect to be using.

Upvotes: 1

rmlan
rmlan

Reputation: 4657

You have a rogue jar with Xerces classes in it.

Check your installed JRE's lib directory. It appears that it is loading the class in question from a jar called xml.jar. I'm not sure where this jar came from as I do not see it in any Java installations I am currently working with (Java 8 on both Linux an OSX).

If you delete (or move) this jar, the problem should be resolved. Note that this may have unintended consequences on other Java programs on your system.

Upvotes: 1

Related Questions