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.reflectTest (com.wy.reflect.reflectTest 就是找不到那個方法的類)
addSrc.jsp 代碼如下:
<%@page contentType="text/html; charset=GBK"%> <%@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) { out.print("類" + className + "實例的物理文件位於:"); out.print("<hr>"); out.print(classLocation); } else { out.print("類" + className + "沒有對應的物理文件。<br>"); out.print("錯誤:" + error); } }catch(Exception e) { out.print("異常。"+e.getMessage()); } %> </body> </html>
目錄截圖:
效果圖:
————————————————
版權聲明:本文為CSDN博主「麻辣你個王子」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/qq_28675967/article/details/90579636