NoSuchMethodError 字面意思没有找到类中的方法,但是一般在项目中都通过定位都能找到对应的方法。很难排查出问题所在。
但是这个问题确实是方法找不到抛出的。一般都是因为JVM全盘委托机制 类加载器加载了一个低版本的jar包,然后却使用了高版本jar包的方法,在程序允许的时候抛出了这样的异常。
所以遇到这样的异常首先排查jar包是否冲突。
如果类路径比较多杂
以下的方法可以通过类查看所在的jar包的路径:
Class cls = null;
try {
cls = Class.forName("com.test.domain.Test");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
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);
}
System.out.println("指定的类jar包路径 " + result);
相关推荐
在Java编程中,`java.lang.NoSuchMethodError`异常是一个运行时异常,通常发生在程序试图执行一个不存在的方法时。这个异常的出现通常意味着编译时和运行时的类版本不匹配,或者是在编译期间存在而运行时不存在的...
在Java开发中,`NoSuchMethodError`是一个常见的运行时异常,通常发生在试图调用一个不存在的方法时。这个错误与JVM的类加载机制密切相关。本文将深入探讨`NoSuchMethodError`,并结合“jar-duplicate-test”这个...
在Java编程中,`java.lang.NoSuchMethodError`异常是一个运行时异常,通常发生在尝试执行一个在编译时可用但在运行时不可用的方法时。这个异常表明某个已经加载的类定义了一个方法,但该方法的实际行为无法被找到。...
Java中的NoSuchMethodError是一个运行时错误,主要在程序尝试调用一个类中不存在的方法时发生。这个错误通常涉及以下几个方面:类的定义不一致、库版本冲突或者类加载问题。当出现NoSuchMethodError时,我们需要仔细...
《系统稳定性——NoSuchMethodError常见原因及解决方法》 NoSuchMethodError是Java编程中常见的运行时错误,当应用程序尝试调用一个类的特定方法,但该类已经不再包含该方法的定义时,就会抛出这个错误。这通常发生...
解决 java.lang.NoSuchMethodError 的错误 Java.lang.NoSuchMethodError 错误是一种常见的 Java 异常,它发生在 Java 虚拟机 (JVM) 无法找到某个类的特定方法时。这种错误可能是由于项目依赖比较复杂、Java 运行...
NULL 博文链接:https://albert0707.iteye.com/blog/562969
1) Error injecting constructor, java.lang.NoSuchMethodError: org.apache.maven.model.validation.DefaultModelValidator: method 'void ()' not found at org.jetbrains.idea.maven.server.embedder....
在Java编程中,`NoSuchMethodError` 是一个常见的运行时异常,它表示在类加载时找到了该类,但在该类中却找不到特定方法的定义。这个问题通常发生在不同版本的库之间存在不兼容的情况,即在编译时使用的库版本包含了...
在Java中,有时我们可能会遇到`java.lang.NoSuchMethodError`,这通常发生在运行时尝试执行一个在编译时存在但在运行时不存在的方法。对于这个问题,CGlib提供了解决方案。CGlib使用ASM库(一个Java字节码操纵和分析...
如果Y包使用了2.5版本中特有的新方法,而在实际运行时系统使用的是2.0版本,那么就会出现NoSuchMethodError错误。 值得注意的是,并非所有的冲突都会导致运行时问题。大多数情况下,即使存在冲突,只要两个版本的...
用于解决struts2升级至2.3.37时 出现java.lang.NoSuchMethodError: com.opensymphony.xwork2.ActionContext.put(Ljava/lang/异常时,更换的xwork-core-2.3.37.jar
NULL 博文链接:https://yangzb.iteye.com/blog/265212
Android XmlResourceParser出错解决办法...java.lang.NoSuchMethodError: No interface method i()I in class Landroid/content/res/XmlResourceParser; or its super classes (declaration of 'android.content.res.Xm
描述中提到的"NoSuchMethodError setCharacterEncoding(Ljava/lang/String;)V"是一个Java运行时异常,意味着在类装载时尝试调用的方法在该类的Class文件中存在,但在链接阶段找不到。这通常发生在试图执行的方法在...