Search code examples
javajava-8runtime-error

Issue with NoSuchMethodException due to Java upgrade


In our application, we are making use of the elementIsInEncryptionSpace method from com.sun.org.apache.xml.internal.security.utils.XMLUtils class, which was available in OpenJDK 1.8.0_292 in the rt.jar (runtime jar).

However, our server Java version got upgraded to OpenJDK 1.8.0_362, in this rt.jar this method is missing.

I'm also using xmlsec.jar in my application classpath which contains XMLUtils.elementIsInEncryptionSpace method, but as rt.jar already contains XMLUtils.class file without the method, my application still throws a NoSuchMethodException.

I found lots of links which suggested not to make any changes to rt.jar ever. Any suggestion to how I can resolve the issue. Thanks in advance for your inputs.


Solution

  • The code for that method is very simple:

    /**
     * Returns true if the element is in XML Encryption namespace and the local
     * name equals the supplied one.
     *
     * @param element
     * @param localName
     * @return true if the element is in XML Encryption namespace and the
       local name equals the supplied one
     */
    public static boolean elementIsInEncryptionSpace(Element element, String localName) {
        if (element == null) {
            return false;
        }
        return EncryptionConstants.EncryptionSpecNS.equals(element.getNamespaceURI())
            && element.getLocalName().equals(localName);
    }
    

    ... where

    public static final String EncryptionSpecNS = "http://www.w3.org/2001/04/xmlenc#";
    

    You should be able to duplicate that functionality in your own codebase.


    For the record, it is and always has been a bad idea to write your code to depend on internals of the JDK. It is liable to break without any prior notice or even mention in release notes, etc ... as has happened here.

    You think it is difficult now? Just wait until you get to Java 17!