How to debug NoSuchMethodError exception?

江枫思渺然 提交于 2019-12-01 04:11:20

问题


I am running the following code

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");
    }

}

and getting

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)

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?


回答1:


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);

Sample output:

jar:file:/D:/maven_repository/commons-io/commons-io/2.0.1/commons-io-2.0.1.jar!/org/apache/commons/io/FileUtils.class



来源:https://stackoverflow.com/questions/29474509/how-to-debug-nosuchmethoderror-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!