{"id":129,"date":"2015-11-27T13:19:01","date_gmt":"2015-11-27T12:19:01","guid":{"rendered":"https:\/\/overdespotiet.dk\/blog\/?p=129"},"modified":"2024-06-19T13:34:47","modified_gmt":"2024-06-19T11:34:47","slug":"tracking-down-a-nosuchmethoderror","status":"publish","type":"post","link":"https:\/\/overdespotiet.dk\/blog\/?p=129","title":{"rendered":"Tracking down a NoSuchMethodError"},"content":{"rendered":"<pre class=\"brush: text; gutter: false\">Caused by: java.lang.NoSuchMethodError: org.apache.xml.serializer.Encodings.isRecognizedEncoding(Ljava\/lang\/String;)Z\r\n        at org.apache.xml.serializer.dom3.LSSerializerImpl.write(LSSerializerImpl.java:926)\r\n        at ...<\/pre>\n<p>If you&#8217;ved ever had the unpleasant task of figuring out which of the 200+ JAR dependencies in your project that are causing the a NoSuchMethodError (usually in some XML or webservice code), then this is the trick for you:<!--more--><\/p>\n<p>Consider the following POM (distilled\u00a0from a real-world scenario):<\/p>\n<pre class=\"brush: xml; gutter: false\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\r\n&lt;project xmlns=\"http:\/\/maven.apache.org\/POM\/4.0.0\"\r\n         xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\r\n         xsi:schemaLocation=\"http:\/\/maven.apache.org\/POM\/4.0.0 http:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd\"&gt;\r\n    &lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;\r\n\r\n    &lt;groupId&gt;net.bystrup&lt;\/groupId&gt;\r\n    &lt;artifactId&gt;nsm-example&lt;\/artifactId&gt;\r\n    &lt;version&gt;1.0-SNAPSHOT&lt;\/version&gt;\r\n\r\n    &lt;dependencies&gt;\r\n        &lt;dependency&gt;\r\n            &lt;groupId&gt;xalan&lt;\/groupId&gt;\r\n            &lt;artifactId&gt;xalan&lt;\/artifactId&gt;\r\n            &lt;version&gt;2.7.0&lt;\/version&gt;\r\n        &lt;\/dependency&gt;\r\n        &lt;dependency&gt;\r\n            &lt;groupId&gt;xalan&lt;\/groupId&gt;\r\n            &lt;artifactId&gt;serializer&lt;\/artifactId&gt;\r\n            &lt;version&gt;2.7.1&lt;\/version&gt;\r\n        &lt;\/dependency&gt;\r\n    &lt;\/dependencies&gt;\r\n&lt;\/project&gt;<\/pre>\n<p>When parsing an XML document, the error\u00a0at the top of the post might occur&#8230; You know the symptom, you know the probable root cause &#8211; but how do you find the\u00a0conflicting JARs?<\/p>\n<p><b><tt>ClassLoader.getResources()<\/tt><\/b> to the rescue! The following snippet will list all resources\/JARs containing the clas with the missing method:<\/p>\n<pre class=\"brush: java; gutter: false\">import java.net.URL;\r\nimport java.util.Enumeration;\r\n\r\npublic class Main\r\n{\r\n    \/**\r\n     * The project depends on both xalan:2.7.0 and serializer:2.7.1 which both\r\n     * contain the org.apache.xml.serializer.Encodings class, albeit with different\r\n     * signatures...\r\n     *\r\n     * @param argv\r\n     *\/\r\n    public static void main( String argv[] ) throws Exception\r\n    {\r\n        Enumeration&lt;URL&gt; res = new Main().getClass().getClassLoader().getResources( \"org\/apache\/xml\/serializer\/Encodings.class\" );\r\n        while ( res.hasMoreElements() )\r\n        {\r\n            URL url = res.nextElement();\r\n            System.out.println( url.toExternalForm() );\r\n        }\r\n    }\r\n}<\/pre>\n<p>The operative\u00a0piece of code<\/p>\n<pre class=\"brush: java; gutter: false\">this.getClass().getClassLoader().getResources( \"org\/apache\/xml\/serializer\/Encodings.class\" )<\/pre>\n<p>might even be evaluated in eg. IDEAs &#8220;Evaluate Expression&#8221; window (Alt-F8) if\u00a0you&#8217;re in a debugging situation.<\/p>\n<p>The only problem remaining is: How does one remember where to find this guide next time the problem occurs&#8230; \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Caused by: java.lang.NoSuchMethodError: org.apache.xml.serializer.Encodings.isRecognizedEncoding(Ljava\/lang\/String;)Z at org.apache.xml.serializer.dom3.LSSerializerImpl.write(LSSerializerImpl.java:926) at &#8230; If you&#8217;ved ever had the unpleasant task of figuring out which of the 200+ JAR dependencies in your project that are causing the a NoSuchMethodError (usually in some XML or webservice code), &hellip; <a href=\"https:\/\/overdespotiet.dk\/blog\/?p=129\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,29],"tags":[],"class_list":["post-129","post","type-post","status-publish","format-standard","hentry","category-coding","category-java"],"_links":{"self":[{"href":"https:\/\/overdespotiet.dk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/129","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/overdespotiet.dk\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/overdespotiet.dk\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/overdespotiet.dk\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/overdespotiet.dk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=129"}],"version-history":[{"count":5,"href":"https:\/\/overdespotiet.dk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/129\/revisions"}],"predecessor-version":[{"id":243,"href":"https:\/\/overdespotiet.dk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/129\/revisions\/243"}],"wp:attachment":[{"href":"https:\/\/overdespotiet.dk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/overdespotiet.dk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/overdespotiet.dk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}