I'm working on a project that needs to utilize external classes. A few years ago I used Class.forName to do this and I remember using an interface. However, I can't remember how this worked. Can someone tell me if they see any problems with the code below:
//Main.java
package directlink; public class Main { Main() { try { Class c = Class.forName("directlink.classes.Dfile"); } catch (ClassNotFoundException x) { x.printStackTrace(); } } public static void main(String[] args) { new Main(); } }
//Command.java
package directlink.classes; import java.util.Vector; abstract class Command implements Commandinterface { Vector<String> options; Command() { this.options = new Vector<String>(10, 10); } public void setOptions(Vector<String>options) { this.options = options; } }
//Commandinterface.java
package directlink.classes; import java.util.Vector; public interface Commandinterface { public void setOptions(Vector<String> name); public Vector<String[]> getActions(); public void runAction(String action); }
//Dfile.java
package directlink.classes; import java.util.Vector; public class Dfile extends Command implements Commandinterface { public Vector<String[]> getActions() { Vector<String[]> actions = new Vector<String[]>(5, 5); String[] command = new String[2]; command[0] = "copy"; command[1] = "from to"; actions.add(command); command = new String[2]; command[0] = "delete"; command[1] = "from"; actions.add(command); return actions; } public void runAction(String action) { if (action.equals("copy")) { //This is just to make sure Class.forName is working this is not a real command System.out.println("Copying file from to"); } } }
and this loads without error. However if I try to use:
c.runAction("copy");
I get the error:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code
at directlink.Main.<init>(Main.java:12)
at directlink.Main.main(Main.java:19)
Java Result: 1