• [java]dynamic class loading
    8 replies, posted
Ok, I have been looking online now, but I can't seem to find a tutorial for exactly what I want to do... Could someone please assist? Essentially I want to create a program that will load all classes inside a jar and invoke a function (load()) in each of them. It will not have a hard set list but will dynamically load all classes on runtime.
It's a while since i've done anything with Java, but you might want to take a look [url=http://download.oracle.com/javase/1.4.2/docs/api/java/lang/reflect/package-summary.html]here[/url]
that looks very close to what i am looking for, i will look into manipulation of it... thanks
as long as they don't have constructors it's as easy as Class.forName( ... ).newInstance(); and it'll work best if you have all your classes inheriting from an abstract class or if they all implement and interface.
thanks :)
If you're interested, here is the chat command dynamic loader for my custom minecraft server. [code] static Map<String,ArrayList<Command>> commands = new HashMap<String,ArrayList<Command>>(); public static void RegisterPlugins() throws InstantiationException, IllegalAccessException, ClassNotFoundException{ ClassLoader cloader = ClassLoader.getSystemClassLoader(); File directory = new File("./src/Server/Events/ChatCommands"); for(File f:directory.listFiles()){ if(f.isFile()&&!f.getName().contains("ommand.")&&!f.getName().contains("andler.")&&f.getName().startsWith("plugin_")){ System.out.println(f.getName()); Class ccl = cloader.loadClass("Server.Events.ChatCommands."+f.getName().replace(".java", "")); Command cmd = (Command) ccl.newInstance(); ArrayList<Command> blah = new ArrayList<Command>(); if(!commands.containsKey(cmd.GetCmd())){ Utils.Logger.Log("Command not contained. Adding."); blah.add(cmd); commands.put(cmd.GetCmd(), blah); } else{ Utils.Logger.Log("Command contained. Adding."); blah = (ArrayList<Command>) commands.get(cmd.GetCmd()).clone(); blah.add(cmd); commands.put(cmd.GetCmd(), blah); } } } } [/code] It's a multimap because I want more than 1 command to be able to register the same chat command. Like /jump, and /jump 40 for specified and nonspecified height.
ah, that could help quite nicely, thanks!
Reflection is the technology you want to use, if you didn't know. [url]http://download.oracle.com/javase/tutorial/reflect/class/index.html[/url] ClassLoader is the specific thing used in that example.
[QUOTE=bobthe2lol;28308738]If you're interested, here is the chat command dynamic loader for my custom minecraft server. [code] static Map<String,ArrayList<Command>> commands = new HashMap<String,ArrayList<Command>>(); public static void RegisterPlugins() throws InstantiationException, IllegalAccessException, ClassNotFoundException{ ClassLoader cloader = ClassLoader.getSystemClassLoader(); File directory = new File("./src/Server/Events/ChatCommands"); for(File f:directory.listFiles()){ if(f.isFile()&&!f.getName().contains("ommand.")&&!f.getName().contains("andler.")&&f.getName().startsWith("plugin_")){ System.out.println(f.getName()); Class ccl = cloader.loadClass("Server.Events.ChatCommands."+f.getName().replace(".java", "")); Command cmd = (Command) ccl.newInstance(); ArrayList<Command> blah = new ArrayList<Command>(); if(!commands.containsKey(cmd.GetCmd())){ Utils.Logger.Log("Command not contained. Adding."); blah.add(cmd); commands.put(cmd.GetCmd(), blah); } else{ Utils.Logger.Log("Command contained. Adding."); blah = (ArrayList<Command>) commands.get(cmd.GetCmd()).clone(); blah.add(cmd); commands.put(cmd.GetCmd(), blah); } } } } [/code] It's a multimap because I want more than 1 command to be able to register the same chat command. Like /jump, and /jump 40 for specified and nonspecified height.[/QUOTE] I don't mean to be touchy, but: dear god that code. :gonk: How do you even maintain something like that.
Sorry, you need to Log In to post a reply to this thread.