javaclassloaderguavabungeecord

Java how to use multiple versions of one library maybe using classloaders


I am developing a java plugin (for BungeeCord) which uses Methods from Guava 28.2. But BungeeCord, the programm that actually loads my plugin, only uses Guava 21. Therefor my plugin crashes since it can't find some methods from Guava 28.2.

Is there a way to make my plugin load its own version of guava and use this version instead of the old one? Can this be done using classloader magic?

Any help is appreciated


Solution

  • No classloader magic needed. The wanted mechanism from maven is called relocation. Relocations can change the package of a library at compile time to prevent conflicts. Using this you can relocate com.google.commons to my.shaded.version.com.google.commons for example and prevent conflicts.

    https://maven.apache.org/plugins/maven-shade-plugin/examples/class-relocation.html