Question

I'm using Tomcat 7 and am learning JSP. I am trying to build a list of files in a directory with a specific extensions. I found this tutorial, and I have the following code:

package winning;

import java.io.File;
import java.io.FileFilter;
import java.util.List;
import java.util.ArrayList;

public class Winning {
    public List<String> getNames(String directory, String extension){
        final String ext = extension;
        File f = null;
        File[] names;
        List<String> results = new ArrayList<String>();

        f = new File(directory);

        FileFilter filter = new FileFilter() {
            @Override
            public boolean accept(File pathname){
                return true;
            }
        };

        names = f.listFiles(filter);

        for(File file : names){
            results.add(file.getName());
        }

        return results;
    }
}

The exception that Tomcat presents is NoClasDefFoundError, but it reports that a ClassNotFoundException is being thrown at the FileFilter filter = new FileFilter... line.

My code works perfectly fine if I get rid of that block, so I have:

...
f = new File(directory);
// used to be code here
names = f.listFiles(/*no more filter*/);
...

It looks to me like basically have the same code as the example, but it's not working. Is this tutorial just really out dated, or is there a way to use an anonymous class here?

Was it helpful?

Solution

When you compile a class that contains anonymous classes, there are multiple .class files generated. For example, you would have Winning.class for the top level class and Winning$1.class for the first anonymous inner class.

If you only put Winning.class in /WEB-INF/classes, then you would get a ClassNotFoundException when the code tries to load the anonymous class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top