I am trying to invoke the method listFilesAndDirs() of org.apache.commons.io.FileUtils using my own custom class loader. But it returns NoSuchMethodException.
Code used for method invoke.
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 });
Code used for class loader
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; } } }
call of cls.getDeclaredMethods() will return the method org.apache.commons.io.FileUtils.listFilesAndDirs(j ava.io.File,org.apache.commons.io.filefilter.IOFil eFilter,org.apache.commons.io.filefilter.IOFileFil ter)
but cls.getMethod("listFilesAndDirs",new Class[] { File.class, IOFileFilter.class, IOFileFilter.class }); returns the following error
java.lang.NoSuchMethodException: org.apache.commons.io.FileUtils.listFilesAndDirs(j ava.io.File, org.apache.commons.io.filefilter.IOFileFilter, org.apache.commons.io.filefilter.IOFileFilter) at java.lang.Class.getDeclaredMethod(Class.java:1937) at Sample.main(Sample.java:30)