25 Apr, 2012, Nathan wrote in the 1st comment:
Votes: 0
I realize this is somewhat related to the other topic in here, but's that's old and dead. So, anyway:

I have been working on writing a mud server in Java and for a while I have been using a big if, else loop for the command interpreter. Basically, for example "if(cmd.equals("where") == true) { cmd_where(argument, client); }". I have recently been trying to convert to a different system. That is, instead of checking the string being equal to some static string, I have some objects which extend an abstract Command class which I then load into a HashMap with a String key and some subclass of Command as the value. Then I can simply ask the hashmap if it has an object for the key that is the command input and call that Command object's method, "execute(String arg, Client client)".

However, in the process I realized it might be interesting and/or useful to be able to load and unload commands, so they could be edited, "recompiled", and then re-loaded. This should be possible any time the changes don't require me to modify any other classes and I have simply have a hashmap of String key -> Command commandObject, since if I am not actually executing the command, I should be able to remove a Command object from the table. After being removed, the interpreter would begin to treat it as a non-existent command or maybe spit some error about it being unloaded if I had a list of commands that should exist.

My problem is that I am unsure how to implement/create a classloader to do what I want. I would like to have control over which commands are loaded at startup as well as be able to unload and re-load commands on the fly. Help?