Using external libraries

Discussion in 'Plugin Development' started by dannycrafts, Feb 5, 2012.

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

    dannycrafts

    Hello,

    I want to use an external library for one of my plugins. The lib is not a plugin, if it was, I would know how I could make bukkit load it into memory. But how do I load any other jar file into memory so that I could use its classes and functions?

    Thank you.
     
  2. Offline

    SgtStud

    I assume this would be the exact same way you loaded Bukkit into your plugin?

    Right click project, hit properties, click on java build path, and then add external jars.
     
  3. Offline

    dannycrafts

    I know how to do that, but how do I 'make it work at runtime'? I don't know how to load the external library into memory, because else it just would throw ClassNotFoundException's.
     
  4. You mean you want your plugin to actually load additional libraries like sub-plugins ?
    If that's the case, see Citizens's source, it uses separate non-bukkit plugin JARs for each NPC type.
     
  5. Offline

    dannycrafts

    It is not that they are plugins of my plugin, but more like a dependency. Anyway, I'll look at Citizens source.
     
  6. Offline

    MuisYa

    Also make sure that if you try to use Citizens for youre plugin please check if the plugin is in the plugin folder.
    Code:
    // put this in the Main class.
     
    public void onEnable() {
        PluginManager pm = Bukkit.getPluginManager();
        if (pm.getPlugin("Citizens") == null) {
            Bukkit.getLogger().info("Citizens is not found, disabling plugin.");
            pm.disablePlugin(this);
        }
        Bukkit.getLogger().info(this + " has been enabled.");
    }
     
  7. MuisYa that's not related, I pointed him to look at a feature of Citizens, not to import it.
     
  8. Offline

    fullwall

  9. As far as I can tell most of you might be off the mark with help.

    dannycrafts will this be for a plugin system (ala Citizens style) or a pre existing library (an sql driver jar, etc)
     
  10. Offline

    dannycrafts

    Yes, still have to look at citizens code though.
     
  11. Offline

    psanker

    You could use a ClassLoader, or you can build the project using Maven (IF that library is available through Maven).
     
  12. Offline

    desht

    You have three options:
    1. Use Maven as psanker said, and shade the library into your plugin. This is the best option. You can still do this if the library in question isn't available on a public Maven repo - just download and inject it into your local Maven repo with the install:install-file goal.
    2. Tell your plugin users they need to download your library (or add code in your plugin to download it) to the bukkit/lib directory. You will need to include an updated MANIFEST.MF file in your plugin which sets the class path to ../lib (see http://docs.oracle.com/javase/tutorial/deployment/jar/downman.html) to ensure that your plugin can see it.
    3. Use ClassLoaders, as others have suggested. This tutorial is good: http://tutorials.jenkov.com/java-reflection/dynamic-class-loading-reloading.html
     
Thread Status:
Not open for further replies.

Share This Page