Search code examples
javapluginsclasspathclassloader

How to prevent third-party JARs on the classpath from overriding my class dependencies?


I am writing an application plugin in Java, and my plugin has dependencies on several third-party JARs. I am bundling these dependencies with my plugin so that I can deploy just a single JAR file.

The host application may also be running plugins from other vendors. Unfortunately the host application puts all the plugins on the classpath, and I am not able to change this behavior. If another vendor's plugin is loaded before mine and uses an incompatible version of a dependency, my plugin could crash.

I am not able to test compatibility between my plugin and other plugins ahead of time. It is also not acceptable for me to say that there is an incompatibility between the plugins--if my plugin crashes, it reflects poorly on my company. The customer does not care why my plugin crashes, they will attribute it to poor programming on my end.

I am looking for a way to prevent other vendors' plugins from interfering with my own. Is it possible?

I've heard of custom classloaders but I'm not sure if that solution will work for me.


Solution

  • You can use Uberjar. What it does is move all your jars/classes to a custom namespace so that none of your classes clash because your dependencies have a different namespace.

    You might want to look at maven-shade