Search code examples
javaclassclassloader

Different versions of a library loaded to the same ClassLoader


Heyho,

Let's say we have this setup:

Application -> plugin -> module

Where "plugin" and "module" depend on "application".

Application uses the version 1.0 of a library. Module depends on the same library, but version 2.0. The classes are the same, but some methods are removed in 2.0 and a few added. Plugin uses the parent ClassLoader of application and module the parent ClassLoader of plugin.

Now the problem is that module uses version 1.0 of the library, but it depends on 2.0 -> for example method not found

What would be the propper way to fix this? Something that would work is to relocate the version 2.0, but maybe there is a workaround during runtime. Maybe it is possible to change the classloaders to fix the problem.

Max


Solution

  • Your options are:

    1. upgrade the application to 2.0
    2. downgrade the plugin to 1.0 (and hope it still works)
    3. move the application "core" into a nested classloader
    4. change the plugin/module classloaders so that they are not "parent first". note that this is a very sketchy option which can really mess things up if not done correctly.

    Option 3. would look like:

    Application Base -> plugin -> module (lib 2.0)
                     -> Application Core (lib 1.0)
    

    This essentially makes the plugin and the application core peers and therefore there is no longer a classloader issue.