How do I load instances of a certain class from a java directory?
<p>Here's my method:</p>

<p>Feature[] getFeatures(File dir)</p>

<p>I'm trying to go through the directory, check each class file. Any class that is of type 'Feature', I want to load an instance of it. Then I want to return an array of these instances.</p>

<p>How is this done?</p>

<p>Thank you.</p>

<p>EDIT:</p>

<p>Here's what I have now:</p>

<pre><code>private static LinkedList&lt;Feature&gt; getFeaturesInDirectory(File dir) throws ClassNotFoundException {

        LinkedList&lt;Feature&gt; features = new LinkedList&lt;Feature&gt;();

        ClassLoader cl = new IndFeaClassLoader();

        // list all files in directory
        for(String s : dir.list()) {
            Class klass = cl.loadClass(s);
            try {
                Object x = klass.newInstance();
                if (x instanceof Feature) {
                    features.add((Feature)x);
                }
            } catch (InstantiationException ex) {
            } catch (IllegalAccessException ex) {
            }

        }

        return features;

    }
</code></pre>
