{"id":59778,"date":"2020-08-19T06:20:24","date_gmt":"2020-08-19T06:20:24","guid":{"rendered":"https:\/\/www.baeldung.com\/?p=85956"},"modified":"2020-08-19T06:20:24","modified_gmt":"2020-08-19T06:20:24","slug":"nosuchmethoderror-in-java","status":"publish","type":"post","link":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/2020\/08\/19\/nosuchmethoderror-in-java\/","title":{"rendered":"NoSuchMethodError in Java"},"content":{"rendered":"<p class=\"syndicated-attribution\"><meta name= \\\"keywords \\\" content= \\\"\u96fb\u5b50\u8a08\u7b97\u6a5f, \u6559\u80b2, IT \u96fb\u8166\u73ed,\u96fb\u8166\u88dc\u7fd2\uff0c \u96fb\u8166\u73ed\uff0c \u5bb6\u6559\uff0c \u79c1\u4eba\u8001\u5e2b\uff0c \u8cc7\u8a0a\u6280\u8853\uff0c \u7a0b\u5e8f\u8a2d\u8a08\uff0c \u96fb\u5b50\u8a08\u7b97\u6a5f\uff0c \u904a\u6232\uff0c \u860b\u679c\uff0c \u96fb\u5f71\uff0c \u8a08\u7b97\u6a5f\uff0c\u7de8\u78bc\uff0c Java\uff0c C\/C++\uff0c JavaScript\uff0c PHP\uff0c HTML\uff0c CSS\uff0c MySQL\uff0c mobile\uff0c Android\uff0c \u52d5\u6f2b\uff0c Python\uff0c teacher\uff0c \u88dc\u7fd2\uff0c \u96fb\u8166\u88dc\u7fd2 \u8cc7\u8a0a, \u7535\u5b50\u8ba1\u7b97\u673a, IT ,Game, apple, movie, Computer,student,Java,\u6559\u80b2, ,\u5b66\u751f, \u5b66\u4e60, learn, \u6559\u5b66,  Android, apple,anime, animation, \u4fe1\u606f\u6280\u672f, \u7a0b\u5e8f\u8bbe\u8ba1, \u79fb\u52a8\u7535\u8bdd, \u8cc7\u8a0a\u79d1\u6280,Game, Jeu, Juego,Call Of Duty ,\u4f7f\u547d\u53ec\u559a , \u6e38\u620f, \u7535\u5b50\u6e38\u620f,, \u591a\u4eba\u7535\u5b50\u6e38\u620f, \u7f51\u7edc\u6e38\u620f\uff0conline\uff0conline game, \u624b\u673a\u6e38\u620f, mobile \\\"><\/p>\n<h2 data-id=\"overview-5\">1. Overview<\/h2>\n<div class=\"bd-anchor\" id=\"overview-5\"><\/div>\n<p>In this tutorial, we&#8217;ll look at the <em>java.lang.NoSuchMethodError <\/em>and some ways to handle it.<\/p>\n<h2 data-id=\"nosuchmethoderror\">2. <em>NoSuchMethodError<\/em><\/h2>\n<div class=\"bd-anchor\" id=\"nosuchmethoderror\"><\/div>\n<p>As the name suggests, <strong>the <em>NoSuchMethodError<\/em> occurs when a particular method is not found<\/strong>. This method can either be an instance method or a static method.<\/p>\n<p><strong>In most cases,<\/strong> <strong>we&#8217;re able to catch this error at compile-time. Hence<\/strong>, it&#8217;s not a big issue. However,<strong> sometimes it could be thrown at runtime<\/strong>, then finding it becomes a bit difficult. According to the <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/lang\/NoSuchMethodError.html\">Oracle documentation<\/a>, this error may occur at runtime if a class has been incompatibly changed.<\/p>\n<p>Hence, we may encounter this error in the following cases. Firstly, <strong>if we do just a partial recompilation<\/strong> of our code.\u00a0Secondly, if there is <strong>version incompatibility with the dependencies<\/strong> in our application, such as the external jars.<\/p>\n<p>Note that the <em>NoSuchMethodError<\/em> inheritance tree includes\u00a0<em><a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/lang\/IncompatibleClassChangeError.html\">IncompatibleClassChangeError<\/a>\u00a0and\u00a0<a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/lang\/LinkageError.html\">LinkageError<\/a>.<\/em> These errors are associated with an\u00a0incompatible class change after compilation.<\/p>\n<h2 data-id=\"example-of-nosuchmethoderror\">3. Example of <em>NoSuchMethodError<\/em><\/h2>\n<div class=\"bd-anchor\" id=\"example-of-nosuchmethoderror\"><\/div>\n<p>Let\u2019s see this error in action with an example. For this, we&#8217;ll create two classes. First is <em>SpecialToday <\/em>which will list the specials for the day in a restaurant:\u00a0<em><br \/>\n<br \/>\n<\/em><\/p>\n<pre><code class=\"language-java\">public class SpecialToday {\r\n    private static String desert = \"Chocolate Cake\";\r\n    public static String getDesert() {\r\n        return desert;\r\n    }\r\n}<\/code><\/pre>\n<p>The second class <em>MainMenu<\/em> calls methods from <em>SpecialsToday:<br \/>\n<br \/>\n<\/em><\/p>\n<pre><code class=\"language-java\">public class MainMenu {\r\n    public static void main(String[] args) {\r\n        System.out.println(\"Today's Specials: \" + getSpecials());\r\n    }\r\n    public static String getSpecials() {\r\n        return SpecialToday.getDesert();\r\n    }\r\n}<\/code><\/pre>\n<p>Here the output will be:<\/p>\n<pre><code class=\"language-text\">Today's Specials: Chocolate Cake<\/code><\/pre>\n<p>Next, we\u2019ll delete the method getDesert() in <em>SpecialToday <\/em>and recompile only this updated class. This time when we run our <em>MainMenu, <\/em>we notice the following runtime error:<\/p>\n<pre><code class=\"language-text\">Exception in thread \"main\" java.lang.NoSuchMethodError: SpecialToday.getDesert()Ljava\/lang\/String;<\/code><\/pre>\n<h2 data-id=\"how-to-handle-nosuchmethoderror\">4. How to Handle <em>NoSuchMethodError<\/em><\/h2>\n<div class=\"bd-anchor\" id=\"how-to-handle-nosuchmethoderror\"><\/div>\n<p>Now let&#8217;s see how we can handle this. For the above code, let\u2019s<strong> do a full clean-compile,<\/strong>\u00a0including both the classes. We&#8217;ll notice that the error will be caught while we&#8217;re compiling. If we use<strong> an IDE like Eclipse<\/strong>, <strong>it will be detected even earlier,<\/strong> as soon as we update <em>SpecialsToday<\/em>.<\/p>\n<p>Hence if we run into this error with our applications, as a first step, we&#8217;ll do a full clean compile. With maven, we&#8217;ll run the <em>mvn clean install <\/em>command.<\/p>\n<p>Sometimes the issue is with the external dependencies of our application. In this case, we&#8217;ll first <strong>check the order of the jars<\/strong> in the build path pulled by the classpath loader. And we&#8217;ll trace and update the inconsistent jar.<\/p>\n<p>However, if we still encounter this error at runtime, we&#8217;ll have to dig deeper. We&#8217;ll have to<strong> ensure that the Compile-time and the Runtime classes and jars have the same versions<\/strong>. For this, we can <strong>run the application with -verbose: class option <\/strong>to check the loaded classes. We can run the command as follows:<\/p>\n<pre><code class=\"language-bash\">$ java -verbose:class com.baeldung.exceptions.nosuchmethoderror.MainMenu\r\n[0.014s][info][class,load] opened: \/usr\/lib\/jvm\/java-11-openjdk-amd64\/lib\/modules\r\n[0.015s][info][class,load] opened: \/usr\/share\/java\/java-atk-wrapper.jar\r\n[0.028s][info][class,load] java.lang.Object source: shared objects file\r\n[0.028s][info][class,load] java.io.Serializable source: shared objects file<\/code><\/pre>\n<p>Using this information about all the classes being loaded in the individual jars, during runtime, we can trace the incompatible dependency.<\/p>\n<p>We should also <strong>make sure that there are no duplicate classes<\/strong> in two or more jars. <strong>In most cases, maven will help control conflicting dependencies<\/strong> directly. Furthermore, we can run the <em>mvn dependency: tree<\/em> command to get the dependency tree of our project as follows:<\/p>\n<pre><code class=\"language-bash\">$ mvn dependency:tree\r\n[INFO] Scanning for projects...\r\n[INFO]\r\n[INFO] -------------&lt; com.baeldung.exceptions:nosuchmethoderror &gt;--------------\r\n[INFO] Building nosuchmethoderror 0.0.1-SNAPSHOT\r\n[INFO] --------------------------------[ jar ]---------------------------------\r\n[INFO]\r\n[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ nosuchmethoderror ---\r\n[INFO] com.baeldung.exceptions:nosuchmethoderror:jar:0.0.1-SNAPSHOT\r\n[INFO] \\- org.junit:junit-bom:pom:5.7.0-M1:compile<\/code><\/pre>\n<p>We can check the libraries and their versions in the list generated by this command. Moreover, we can also manage dependencies using maven tags. Using <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/maven-version-collision\">the <em>&lt;exclusions&gt;<\/em> tag<\/a>, we can exclude the problematic dependency. Using <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/maven-optional-dependency\">the <em>&lt;optional&gt;<\/em> tag<\/a>, we can prevent unwanted dependencies from being bundled in the jar or war.<\/p>\n<h2 data-id=\"conclusion-6\">5. Conclusion<\/h2>\n<div class=\"bd-anchor\" id=\"conclusion-6\"><\/div>\n<p>In this article, we addressed <em>NoSuchMethodError<\/em>. We discussed the cause of this error and also ways to handle it. For more details on how to handle errors properly, please refer to our article on <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/java-error-catch\">catching Java errors<\/a>.<\/p>\n<p>As always, the code presented in this article is available <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/github.com\/eugenp\/tutorials\/tree\/master\/core-java-modules\/core-java-exceptions-3\">over on GitHub.<\/a><\/p>\n<p><Img align=\"left\" border=\"0\" height=\"1\" width=\"1\" alt=\"\" style=\"border:0;float:left;margin:0;padding:0;width:1px!important;height:1px!important;\" hspace=\"0\" src=\"https:\/\/feeds.feedblitz.com\/~\/i\/633888956\/0\/baeldung\"><\/p>\n<div style=\"clear:both;padding-top:0.2em;\"><a title=\"Like on Facebook\" href=\"https:\/\/feeds.feedblitz.com\/_\/28\/633888956\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/fblike20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Share on Google+\" href=\"https:\/\/feeds.feedblitz.com\/_\/30\/633888956\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/googleplus20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Pin it!\" href=\"https:\/\/feeds.feedblitz.com\/_\/29\/633888956\/baeldung,\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/pinterest20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Tweet This\" href=\"https:\/\/feeds.feedblitz.com\/_\/24\/633888956\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/twitter20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Subscribe by email\" href=\"https:\/\/feeds.feedblitz.com\/_\/19\/633888956\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/email20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Subscribe by RSS\" href=\"https:\/\/feeds.feedblitz.com\/_\/20\/633888956\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/rss20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a rel=\"NOFOLLOW\" title=\"View Comments\" href=\"https:\/\/www.baeldung.com\/java-nosuchmethod-error#respond\"><img decoding=\"async\" height=\"20\" style=\"border:0;margin:0;padding:0;\" src=\"https:\/\/assets.feedblitz.com\/i\/comments20.png\"><\/a>&#160;<a title=\"Follow Comments via RSS\" href=\"https:\/\/www.baeldung.com\/java-nosuchmethod-error\/feed\/\"><img decoding=\"async\" height=\"20\" style=\"border:0;margin:0;padding:0;\" src=\"https:\/\/assets.feedblitz.com\/i\/commentsrss20.png\"><\/a>&#160;<\/div>\n\n<p class=\"syndicated-attribution\"><figure class= \\\"wp-block-image alignnone \\\"><img src= \\\"http:\/\/itteacheritfreelance.hk\/test\/wordpress\/wp-content\/uploads\/2016\/05\/logo2-2.png\\\" alt=\\\"IT\u96fb\u8166\u88dc\u7fd2 java\u88dc\u7fd2 \u70ba\u5927\u5bb6\u914d\u5c0d\u96fb\u8166\u88dc\u7fd2,IT freelance, \u79c1\u4eba\u8001\u5e2b, PHP\u88dc\u7fd2,CSS\u88dc\u7fd2,XML,Java\u88dc\u7fd2,MySQL\u88dc\u7fd2,graphic design\u88dc\u7fd2,\u4e2d\u5c0f\u5b78ICT\u88dc\u7fd2,\u4e00\u5c0d\u4e00\u79c1\u4eba\u88dc\u7fd2\u548cFreelance\u81ea\u7531\u5de5\u4f5c\u914d\u5c0d\u3002\\\"\/><figcaption>\u7acb\u523b\u8a3b\u518a\u53ca\u5831\u540d\u96fb\u8166\u88dc\u7fd2\u8ab2\u7a0b\u5427!<\/figcaption><\/figure>\r\n<\/br>Find A Teacher Form:\r\n<\/br>https:\/\/docs.google.com\/forms\/d\/1vREBnX5n262umf4wU5U2pyTwvk9O-JrAgblA-wH9GFQ\/viewform?edit_requested=true#responses\r\n<\/br><\/br>Email:\r\n<\/br>public1989two@gmail.com<br><br><br><br><br><br><br>\r\n<a href=www.itsec.hk style=color:#FFFFFF;>www.itsec.hk<\/a><br>\r\n<a href=\\\"www.itsec.vip\\\" style=color:#FFFFFF;>www.itsec.vip<\/a><br>\r\n<a href=\\\"www.itseceu.uk\\\" style=color:#FFFFFF;>www.itseceu.uk<\/a><br><\/p>","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Let&#8217;s look at the java.lang.NoSuchMethodError and some ways to handle it.<\/p>\n<div><a title=\"Like on Facebook\" href=\"https:\/\/feeds.feedblitz.com\/_\/28\/633888956\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/fblike20.png\"><\/a>&nbsp;<a title=\"Share on Google+\" href=\"https:\/\/feeds.feedblitz.com\/_\/30\/633888956\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/googleplus20.png\"><\/a>&nbsp;<a title=\"Pin it!\" href=\"https:\/\/feeds.feedblitz.com\/_\/29\/633888956\/baeldung,\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/pinterest20.png\"><\/a>&nbsp;<a title=\"Tweet This\" href=\"https:\/\/feeds.feedblitz.com\/_\/24\/633888956\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/twitter20.png\"><\/a>&nbsp;<a title=\"Subscribe by email\" href=\"https:\/\/feeds.feedblitz.com\/_\/19\/633888956\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/email20.png\"><\/a>&nbsp;<a title=\"Subscribe by RSS\" href=\"https:\/\/feeds.feedblitz.com\/_\/20\/633888956\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/rss20.png\"><\/a>&nbsp;<a rel=\"NOFOLLOW\" title=\"View Comments\" href=\"https:\/\/www.baeldung.com\/java-nosuchmethod-error#respond\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/comments20.png\"><\/a>&nbsp;<a title=\"Follow Comments via RSS\" href=\"https:\/\/www.baeldung.com\/java-nosuchmethod-error\/feed\/\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/commentsrss20.png\"><\/a>&nbsp;<\/div>\n<\/div>","protected":false},"author":214,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"NoSuchMethodError in Java - ITTeacherITFreelance.hk","description":"Let's look at the java.lang.NoSuchMethodError and some ways to handle it. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"},"footnotes":""},"categories":[6],"tags":[4789],"_links":{"self":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/59778"}],"collection":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/214"}],"replies":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=59778"}],"version-history":[{"count":4,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/59778\/revisions"}],"predecessor-version":[{"id":61900,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/59778\/revisions\/61900"}],"wp:attachment":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=59778"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=59778"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=59778"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}