Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Custom class loader returns NoSuchMethodException in listFilesAndDirs() method call

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Custom class loader returns NoSuchMethodException in listFilesAndDirs() method call

    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)


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,302
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Custom class loader returns NoSuchMethodException in listFilesAndDirs() method call

    Interesting problem...my suspicion is that you have two different ClassLoaders which is causing two different class definitions. The problem then becomes when you're custom ClassLoader reads in a class, it's dependencies may be different than that already on the classpath (in this case, IOFileFilter.class). When you try and call a method, the Class parameters your code defines differs from that of the Class you read in. As evidence, try calling getMethod() on a method that doesn't rely on any of the 3rd party classes, for instance sizeof(). or moveFile()

    Edit: Running this through Eclipse debugger, sure enough if IOFileFilter.class defined from 'IOFileFilter.class' differs from that defined by getParameterTypes() of the corresponding method. Given the code in Class compares using ==, the getMethod search fails. Try defining the class using your ClassLoader:
    Class ioFileFilter = c.loadClass("org.apache.commons.io.filefilter.IOFileFilter");

  3. The Following User Says Thank You to copeg For This Useful Post:

    kavin.gasc (May 19th, 2014)

  4. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,135
    My Mood
    Mellow
    Thanks
    187
    Thanked 660 Times in 647 Posts

    Default Re: Custom class loader returns NoSuchMethodException in listFilesAndDirs() method call

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

Similar Threads

  1. Can't call paint() method from another class
    By Huw in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 18th, 2012, 02:15 AM
  2. Replies: 1
    Last Post: February 12th, 2012, 12:01 AM
  3. Where to define ur Custom Class Loader?
    By tcstcs in forum Java Theory & Questions
    Replies: 4
    Last Post: January 25th, 2012, 05:13 AM
  4. Call method(s) within the same class
    By mwr76 in forum Object Oriented Programming
    Replies: 8
    Last Post: September 26th, 2011, 12:58 AM
  5. [SOLVED] Class loader , InputStream-AudioStream (method) , returns an AudioStream Objet
    By chronoz13 in forum File I/O & Other I/O Streams
    Replies: 14
    Last Post: July 9th, 2011, 11:14 AM

Tags for this Thread