Loading jar files into classloader

Discussion in 'Plugin Development' started by CorrieKay, Nov 7, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    CorrieKay

    I know this is more of a general Java problem, but its a problem that has been solved by bukkit, and the solution will be used by a plugin.

    Like bukkit, i would like my plugin to have a miniature plugin system. These will be modules to a plugin. Think of it similar to essentials, except with a minimal config, and only code you want to run in the plugin.

    That said, im stumped on a few steps.

    1) how do i actually load all of the classes dynamically? I can load the jar file as a url, and get a URLClassLoader, and i know ive done it correctly because i can retrieve resources from said jar file, however, if i try to utilize any classes, i get class not found errors.
    2) saving the generated classloader per module to be able to get resources from that jar seems fairly unintuitive. Is there an easier way to get resources from specific jar files?
     
  2. Offline

    fireblast709

    CorrieKay create a base jar, and let the others use that as dependency. This way the mini-plugins wont load if you do not have the main plugin. Besides you can disable and enable the mini-plugins via your main plugin, using Bukkit.getPluginManager(String plugin_name);
     
  3. Offline

    CorrieKay

    Thats the plan, but as for actually loading the module/mini plugin jar file, i still have an issue with it :p
     
  4. Offline

    fireblast709

    what is the issue? Like errors? (in that case, stacktrace please :3)
     
  5. Offline

    CorrieKay

    when i create a URLClassLoader, it doesnt load any classes. Either that or im having issues with the path to the class file.
     
  6. Offline

    fireblast709

    CorrieKay I don't think you get my idea. Say we have a plugin A, and a mini-plugin B. Plugin B has A as dependency in its plugin.yml, so only having plugin B does not do much. However, if you have A, B will load.
    Then you can use A to say disable or edit B: (assuming B is the main class, code is for A)
    Code:java
    1. B b = (B)Bukkit.getPluginManager().getPlugin("B");
    2. if(b != null)
    3. {
    4. // B is loaded, now you can do, say
    5. Bukkit.getPluginManager().disablePlugin(B); // Disable
    6. Bukkit.getPluginManager().enablePlugin(B); // And enable
    7. // Or edit public field c
    8. b.c = "say c was a String, it would be this right now";
    9. // Or even call method d
    10. b.d(); // Now d() was called
    11. }
     
  7. Offline

    Ranzdo

    When you create an URLClassLoader the classes it loads can only be accessed from the parent ClassLoader by using its methods since creating a URLClassLoader does not mean it will load into the parent's class-path. As you also describe it is not the best idea to save the URLClassLoader and load the classes you want from it.

    This works in Bukkit because they don't need to get any specific classes from URLClassLoader, they only need to get the class that is defined in the plugin.yml, creating a instance of it and then handle it as a JavaPlugin object.

    You need to think abstract like bukkit does. Having a class like JavaPlugin with an onEnable() method that you can call from your load all plugins method so you can start the magic the plugins does without actually reference them.

    Edit: as fireblast709 mentions above, it is a better system to have a main plugin load first and make the mini-plugins depend on them. Since Bukkit loads every plugin into the same class path, you can access all classes from all plugins as long as they are loaded.
     
  8. Offline

    CorrieKay

    My mistake, i didnt explain what i wanted properly.

    The "mini plugins" are not going to be loaded into bukkit as a plugin, but managed purely from within my plugin.

    and yes, i do have a base class that the module's main class must extend.
     
  9. Offline

    fireblast709

    CorrieKay Just a question: why do you want to do it so hard when you can let the main class handle everything using Bukkit
     
  10. Offline

    CorrieKay

    Why not? :p
     
    fireblast709 likes this.
  11. Offline

    md_5

    On a side note, could be worthwhile writing this mini plugins in a scripting language, saves compiling everything.
     
    CorrieKay likes this.
  12. Offline

    Postkutsche

    That's just exactly what I have been looking for yesterday morning. :D
    I found someone who wrote a good example, unfortunatly it's in german: http://entwickler-forum.de/archive/index.php/t-31717.html (3rd post).
    I have shorten the code a bit for my uses but I'm just online with my mobile phone so I can't post it at the moment.
    If you need further explanation (or translation) just tell me. :)
     
    CorrieKay likes this.
  13. Offline

    CorrieKay

    Would definitely appreciate a translation!
     
  14. Offline

    MisterErwin

    Postkutsche: unfotunaltly?!?:D

    Sometimes it's nice, that you can read a text without (very much) thinking...:rolleyes:
     
Thread Status:
Not open for further replies.

Share This Page