NoSuchMethodError 解決方法


NoSuchMethodError 解決方法

java.lang.NoSuchMethodError,想必 Java的開發者都遇到過這個報錯吧,這個錯誤基本上都是由JVM 的 “全網負責委托機制”,(全網負責委托機制是啥? --- 》》 https://cloud.tencent.com/developer/article/1353281

引發的問題, 本人在此奉上三種解決方案:

步驟一:全局搜索該方法是否存在,(目前IDEA可以支持該操作,包括source包均能搜到)如果搜不到那就是真的不存在,一般人不會犯這個錯(除非你不是一般人),如果存在這個方法,看步驟二

*步驟二:*如果是自己項目中自定義的方法,那么執行 clean install 就o的k了,如果這個方法是來自公司的私服或者開源的jar包里面的方法,那么這個時候除了項目clean install外,最好就是去本地倉庫里面把已經下載jar包全部刪除,重新download一遍,基本可以解決問題,如果以上方法還行不通,看步驟三(終極方案)

*步驟三*:上述兩種方案都沒用,極有可能就是某個jar有沖突,引入了多個版本的類包,這個問題的排查是比較棘手的,特別是在web應用的情況下。類路徑的系統目錄比較多,情況尤其復雜,你很難知道JVM到底從哪里類包中加載類文件,針對這個問題,送你一個jsp用來處理這個情況,將 addSrc.jsp 放到Web應用的根路徑下,通過如下方式即可查看JVM從哪個類包中加載的指定類,從而就能排除有沖突的jar包了,親測有效哦:

http://localhost:8081/addSrc.jsp?className=com.wy.reflect.reflectTestcom.wy.reflect.reflectTest 就是找不到那個方法的類)

addSrc.jsp 代碼如下:

<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"%>
<%@page import="java.security.*,java.net.*,java.io.*"%>
<%!
   public static URL getClassLocation(final Class cls) {
       if (cls == null)throw new IllegalArgumentException("null input: cls");
       URL result = null;
       final String clsAsResource = cls.getName().replace('.', '/').concat(".class");
       final ProtectionDomain pd = cls.getProtectionDomain();
       // java.lang.Class contract does not specify if 'pd' can ever be null;
       // it is not the case for Sun's implementations, but guard against null
       // just in case:
       if (pd != null) {
           final CodeSource cs = pd.getCodeSource();
           // 'cs' can be null depending on the classloader behavior:
           if (cs != null) result = cs.getLocation();
           if (result != null) {
               // Convert a code source location into a full class file location
               // for some common cases:
               if ("file".equals(result.getProtocol())) {
                   try {
                       if (result.toExternalForm().endsWith(".jar") ||
                               result.toExternalForm().endsWith(".zip"))
                           result = new URL("jar:".concat(result.toExternalForm())
                                  .concat("!/").concat(clsAsResource));
                       else if (new File(result.getFile()).isDirectory())
                           result = new URL(result, clsAsResource);
                  }
                   catch (MalformedURLException ignore) {}
              }
          }
      }
       if (result == null) {
           // Try to find 'cls' definition as a resource; this is not
           // document.d to be legal, but Sun's implementations seem to         //allow this:
           final ClassLoader clsLoader = cls.getClassLoader();
           result = clsLoader != null ?
                   clsLoader.getResource(clsAsResource) :
                   ClassLoader.getSystemResource(clsAsResource);
      }
       return result;
  }
%>
<html>
<head>
   <title>srcAdd.jar</title>
</head>
<body bgcolor="#ffffff">
使用方法,className參數為類的全名,不需要.class后綴,如
srcAdd.jsp?className=java.net.URL<br><br><br>
<%
   try
  {
       String classLocation = null;
       String error = null;
       String className = request.getParameter("className");

       classLocation =  ""+getClassLocation(Class.forName(className));
       if (error == null) {
           System.out.print("類" + className + "實例的物理文件位於:");
           System.out.print("<hr>");
           System.out.print(classLocation);
      }
       else {
           System.out.print("類" + className + "沒有對應的物理文件。<br>");
           System.out.print("錯誤:" + error);
      }
  }catch(Exception e)
  {
       System.out.print("異常。"+e.getMessage());
  }
%>
</body>
</html>

 

 

目錄截圖:

img

效果圖:

img

這里的異常信息也可以在idea中查看,然后根據異常信息找到對應的jar包,看看里面異常的類class有沒有缺少什么,或者不管有沒有都刪除class所在的異常文件,然后重新運行項目。

可以將異常的.class文件拖至idea中進行反編譯,idea有極強的反編譯功能!!!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM