MyInterface.java
The interface is a stub.
package test; public interface MyInterface { public void printSomething(); }
TestClass.java
A loadable test class that implements your interface.
import test.MyInterface; public class TestClass implements MyInterface { public void printSomething() { System.out.println("Hello World, from TestClass"); } }
(A compiled class file placed in "subfolder /".)
Test.java
A complete test program that downloads all class files from the "subfolder /" and creates instances and runs the interface method on it.
package test; import java.io.File; public class Test { public static void main(String[] args) { try { ClassLoader cl = ClassLoader.getSystemClassLoader(); File subfolder = new File("subfolder"); for (File f : subfolder.listFiles()) { String s = f.getName(); System.out.println("Loading " + s); Class cls = cl.loadClass(s.substring(0, s.lastIndexOf('.'))); MyInterface o = (MyInterface) cls.newInstance(); o.printSomething(); } } catch (ClassNotFoundException e) { } catch (InstantiationException e) { } catch (IllegalAccessException e) { } } }
Conclusion from the test program above:
Download TestClass.class
Hello World, from TestClass
source share