如何调试NoSuchMethodError异常? [英] How to debug NoSuchMethodError exception?
本文介绍了如何调试NoSuchMethodError异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在运行以下代码
package test.commons;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
public class Try_FileUtilsWrite {
public static void main(String[] args) throws IOException {
System.out.println(FileUtils.class.getClassLoader());
FileUtils.write(new File("output.txt"), "Hello world");
}
}
并获得
Exception in thread "main" java.lang.NoSuchMethodError: org.apache.commons.io.FileUtils.write(Ljava/io/File;Ljava/lang/CharSequence;)V
at test.commons.Try_FileUtilsWrite.main(Try_FileUtilsWrite.java:12)
显然,某个地方使用了commons io的旧版本.但是我没有在项目中看到它.
apparently, an old version of commons io used somewhere. But I don't see it in the project.
是否可以在运行时知道类文件的路径?
Is it possible to know the path to class file at runtime?
推荐答案
是的,您可以使用Classloader
获取加载类的资源:
Yes you can use that Classloader
to get the resource from which the class is loaded:
ClassLoader classLoader = FileUtils.class.getClassLoader();
URL resource = classLoader.getResource("org/apache/commons/io/FileUtils.class");
System.out.println(resource);
示例输出:
jar:文件:/D:/maven_repository/commons-io/commons-io/2.0.1/commons-io-2.0.1.jar!/org/apache/commons/io/FileUtils.class
jar:file:/D:/maven_repository/commons-io/commons-io/2.0.1/commons-io-2.0.1.jar!/org/apache/commons/io/FileUtils.class
这篇关于如何调试NoSuchMethodError异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文