gpt4 book ai didi

java - 自定义类加载器在 listFilesAndDirs() 方法调用中返回 NoSuchMethodException

转载 作者:行者123 更新时间:2023-12-01 12:58:03 26 4
gpt4 key购买 nike

我正在尝试使用我自己的自定义类加载器调用 org.apache.commons.io.FileUtils 的 listFilesAndDirs() 方法。但它返回NoSuchMethodException。

用于方法调用的代码。

MyLoader c=new MyLoader();
Class cls=c.loadClass("org.apache.commons.io.FileUtils");
//to display the available methods
Method m[] = cls.getDeclaredMethods();
for (int i = 0; i < m.length; i++)
System.out.println(m[i].toString());

// to get a listFilesAndDirs method
Method me=cls.getMethod("listFilesAndDirs",new Class[] { File.class, IOFileFilter.class,
IOFileFilter.class });

用于类加载器的代码

public class MyLoader extends ClassLoader {
private String classPath;
public MyLoader()
{

}
private String jarFile = "D:/Project/lib/commons-io-2.4.jar";; //Path to the jar file
private Hashtable classes = new Hashtable(); //used to cache already defined classes

public Class loadClass(String className) throws ClassNotFoundException {

return findClass(className);
}

public Class findClass(String className) {
//System.out.println(className+" is loaded by Custom class Loader");


byte classByte[];
Class result = null;

result = (Class) classes.get(className); //checks in cached classes
if (result != null) {
return result;
}



try {
JarFile jar = new JarFile(jarFile);
classPath=className.replaceAll("\\.", "/");
JarEntry entry = jar.getJarEntry(classPath + ".class");
if(entry==null)
{
return findSystemClass(className);
}
else
{

InputStream is = jar.getInputStream(entry);
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
int nextValue = is.read();
while (-1 != nextValue) {
byteStream.write(nextValue);
nextValue = is.read();
}

classByte = byteStream.toByteArray();
result = defineClass(className, classByte, 0, classByte.length, null);
classes.put(className, result);
return result;
}
} catch (Exception e) {
return null;
}


}
}

调用 cls.getDeclaredMethods() 将返回该方法org.apache.commons.io.FileUtils.listFilesAndDirs(java.io.File,org.apache.commons.io.filefilter.IOFileFilter,org.apache.commons.io.filefilter.IOFileFilter)

但是 cls.getMethod("listFilesAndDirs",new Class[] { File.class, IOFileFilter.class, IOFileFilter.class });返回以下错误

java.lang.NoSuchMethodException: org.apache.commons.io.FileUtils.listFilesAndDirs(java.io.File, org.apache.commons.io.filefilter.IOFileFilter, org.apache.commons.io.filefilter.IOFileFilter ) 在 java.lang.Class.getDeclaredMethod(Class.java:1937) 在 Sample.main(Sample.java:30)

最佳答案

所以,getDeclaredMethods()显示相关类具有所需的方法 listFilesAndDirs(java.io.File,org.apache.commons.io.filefilter.IOFileFilter,org.apache.commons.io.filefilter.IOFileFilter) ,

但是

参数类型与您传递给 cls.getMethod("listFilesAndDirs",new Class[]{...}) 的参数类型不同即使它们看起来引用了正确的类。

您编译的代码使用 org.apache.commons.io.filefilter.IOFileFilter来自上下文类加载器的类,而 cls期望来自自定义类加载器的相同类。

使其工作的一种方法是加载 IOFileFilter也来自自定义类加载器,然后在调用 getMethod 时使用该类作为第二项和第三项.

Class ioFileFilterClass = c.loadClass( "org.apache.commons.io.filefilter.IOFileFilter" );
Method me = cls.getMethod( "listFilesAndDirs", new Class[] { File.class, ioFileFilterClass, ioFileFilterClass} );

它将解决这个特定问题,但每次尝试访问自定义类加载器加载的类和方法时,您都必须编写类似的代码。

关于java - 自定义类加载器在 listFilesAndDirs() 方法调用中返回 NoSuchMethodException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23736433/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com