How to debug NoSuchMethodError exception?

I run 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 having received

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 the old version of shared resources was used. But I do not see this in the project.

Is it possible to find out the path to the class file at runtime?

+4
source share
1 answer

Yes, you can use this Classloaderto 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);

Output Example:

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

+7
source

All Articles