How can I load external jars at runtime?

Discussion in 'Spigot Plugin Development' started by Mike_x1, May 10, 2015.

  1. Hello,

    I recently started programming a custom skills plugin for my server.
    Anyway, I neither want to hardcode the skills, nor do I want to load each skill as a separate plugin. So I decided to make each skill a plugin of the plugin/a sub-plugin/however you would call it.

    So basically I want to have a subdirectory of my skill-plugin's folder where all the skills are present as seperate jar files. Each of them would contain a class that would be registered as a listener on the server.

    Now, after I have finished programming the base API for it, I have encountered a problem (which I didn't think of at first to be honest) - I have absolutely no idea on how to load these jar files from the folder and use them inside my plugin.

    I would really appreciate if anyone of you could help me.
    Thanks in advance - Mike_x1
  2. You need to write a custom class loader. Also, you'd need to either iterate through the files or specify the main class for the plugin to be loaded. My suggestion is not to do this as you have no knowledge of class loaders yet, and therefore likely have not too much knowledge of the entire Java language. For now try not to overcomplicate things, and just take an easier approach. I don't want to see a developer get burned out because they took on something too big or hard to complete. That's just my advice though, so you don't have to take it. Anyway if you have any other questions, feel free to ask.
    • Like Like x 1
  3. First of all, thanks for your fast reply :)
    Do you think it would be better to use a complete plugin framework library like jspf or rather implementing the skills directly in the plugin?
  4. Depends on how you're doing it. I myself would create the plugin framework I needed for it, as I like to make things from scratch. That framework seems good, but I don't really know what exactly you're aiming for so I can't be for sure it'll be what you want. The only thing you can really do is try it out if you think you know what you're doing. Look more into the Java Classloader before you start though.