Making plugins which use the same 3rd party packages play nice

Discussion in 'Plugin Development' started by Brettflan, Feb 28, 2011.

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

    Brettflan

    My problem stems from two plugins which both use Gson, namely Factions and WorldGuard (custom build from new-ish source).
    If I have either plugin loaded into CraftBukkit without the other, there's no problem. If I have both of them, WorldGuard loads first with no problem but then Factions fails to load with this error:

    Code:
    java.lang.IllegalAccessError: tried to access field com.google.gson.Gson.DEFAULT_ANON_LOCAL_CLASS_EXCLUSION_STRATEGY from class com.google.gson.GsonBuilder
            at com.google.gson.GsonBuilder.<init>(GsonBuilder.java:93)
            at com.bukkit.mcteam.factions.entities.EM.<clinit>(EM.java:34)
            at com.bukkit.mcteam.factions.Factions.onEnable(Factions.java:34)
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:117)
    <snip>
    I've tried recompiling both plugins with Gson built in, and recompiling both plugins with Gson as a referenced external library JAR in the manifest file (both pointing to the same file). Also with one using an external Gson library JAR and the other using built-in Gson. All of those combinations failed the same way, with Factions giving the above error.

    So... as my experience with Java is limited, can anyone with more experience provide advice on how to get around this sort of conflict? I expect the answer is pretty basic and due to my inexperience with Java, but I'm just about stumped.
     
  2. Offline

    Afforess

    From Sun's (er, Oracle)'s Java Doc's

    IllegalAccessError:Thrown if an application attempts to access or modify a field, or to call a method that it does not have access to.

    Googling exceptions is a huge part to learning how to deal with them. You're trying to access a private or protected field in gson, and that's not allowed.
     
  3. Offline

    Brettflan

    I have used Google, believe it or not. Take your condescension elsewhere. You'll notice it's GsonBuilder from the Gson package itself that is throwing the error:

    java.lang.IllegalAccessError: tried to access field com.google.gson.Gson.DEFAULT_ANON_LOCAL_CLASS_EXCLUSION_STRATEGY from class com.google.gson.GsonBuilder

    That, along with the fact that the error only happens when I load 2 plugins which both use Gson. So what am I actually missing here?
     
  4. Offline

    Afforess

    I wasn't being condesncing, google is the answer to 99/100 answer questions.

    But Ciao.
     
  5. Offline

    Brettflan

    Not that it much matters since you're gone, but fully reading through my first post would have told you that it wasn't quite so simple.


    EDIT:
    Well, it's horribly crude and absolutely not the right way to do it, but I ended up simply renaming the gson source folder to gson2 in one of the projects and did a search-and-replace on the java files to update the package paths accordingly.

    It's ugly, but it works fine and I'm tired of trying to figure out the proper way to handle that sort of clash.
     
  6. Offline

    Raphfrk

    @Brettflan

    I added a pull request that might help with this (and other issues).

    Can you try this modified version of craftbukkit.

    If you would rather compile from source, it is the retryPlugin branch on my github repository.

    It is mainly for allowing dependencies between plugins.

    However, the latest version also updates the search ordering for classes. It will now search the local jar file before checking if the class has already been loaded.
     
  7. Offline

    Brettflan

    OK, I've gone ahead and tested your modified version out. Works great from my brief testing. :)
    I'm also slightly relieved to learn that it's not a problem with how I was compiling the plugins, after all.

    Further details on my test case. I again made builds of each plugin with the default normal package name for Gson (com.google.gson) built into both plugin JAR files. Loading them in CraftBukkit dev build 485 resulted in the usual error above. Loading them in your modified CraftBukkit ended up with both of them loading properly, no loading errors.

    I haven't tested it beyond that, though. I was considering there still being a potential problem with different library version dependencies, i.e. Gson 0.6 built into one plugin and Gson 3.0 in another, but from the sound of it you've got that covered too. Great work. :D
     
  8. Offline

    Raphfrk

    Cool, I'll link to your post as an example of it working :).
     
Thread Status:
Not open for further replies.

Share This Page